Convert List Objects to JSON in Express.js Rest API

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

  • LearnExpressJSRestAPIWithRealApps
    • api
      • demo.api.js
      • index.js
    • server.js
var DemoAPI = {
    findAll: function (request, response) {
        response.setHeader('Content-Type', 'application/json');
        var products = [
            {
                id: 'p01',
                name: 'name 1',
                price: 20
            },
            {
                id: 'p02',
                name: 'name 2',
                price: 21
            },
            {
                id: 'p03',
                name: 'name 3',
                price: 22
            }
        ];
        response.json(products);
    }
};

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

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

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

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