Convert Object to JSON in Express.js Rest API

This project will demonstrate how to Convert Object to JSON in Express.js Rest API

  • LearnExpressJSRestAPIWithRealApps
    • api
      • demo.api.js
      • index.js
    • server.js
var DemoAPI = {
    find: function (request, response) {
        response.setHeader('Content-Type', 'application/json');
        var product = {
            id: 'p01',
            name: 'name 1',
            price: 20
        };
        response.json(product);
    }
};

module.exports = DemoAPI;
var express = require('express');
var router = express.Router();

var DemoAPI = require('./demo.api');

router.get('/demo/find', DemoAPI.find);

module.exports = router;
var express = require('express');
var app = express();

app.use('/api', require('./api/index'));

app.listen(9090, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log("Server listening at http://%s:%s", host, port)
});

Screenshots