Monday, September 1, 2014

read json file and return json file in nodejs

1. at first reading json file
2. json string to object
3. return json



var express = require('express');
var router = express.Router();
var fs = require('fs');

/* GET users listing. */
router.get('/', function(req, res) {
   
   
var text= fs.readFile('./package.json', 'utf-8', function(err, data){

        res.json(JSON.parse(data));
    });

});

module.exports = router;


----------   package.json  --------

{
  "name": "myapp",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "express": "~4.8.6",
    "body-parser": "~1.6.6",
    "cookie-parser": "~1.3.2",
    "morgan": "~1.2.3",
    "serve-favicon": "~2.0.1",
    "debug": "~1.0.4",
    "jade": "~1.5.0",
    "stylus": "0.42.3"
  }
}

No comments:

Post a Comment