Last Updated: February 25, 2016
·
798
· davidgomes

Publishing an object as a JSON file with Node (express.js)

app.get('/result', function(req, res) {
  res.setHeader('Content-Type', 'application/json; charset=utf-8');
  res.end(JSON.stringify({ name: "David", age: 19 }, null, 4));
});

If you want to publish a JavaScript object as a JSON file (kind of like an API), all you have to do is the above. Read more on the JSON.stringify function.

This is really simple yet very useful.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside