Sending JSON data back

The next thing we'd look into is how we can send some JSON data back. Sending JSON is really easy with Express. To illustrate how we can do it we'll comment out our current call to res.send and add a new one. We'll call res.send passing in an object:

app.get('/', (req, res) => {  // res.send('<h1>Hello Express!</h1>');  res.send({  })});

On this object we can provide whatever we like. We can create a name property, setting it equal to the string version of any name, say Andrew. We can make a property called likes, setting it equal to an array, and we can specify some things we may like. Let's add Biking as one of them, and then add Cities as another:

  res.send({    name: 'Andrew',    likes: [      'Biking',      'Cities'    ]  });

When we ...

Get Learning Node.js Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.