Creating a Simple http server in node.js to serve JSON


Overview

Creating a simple http server listening at port 3000 and is used to serve JSON.

Code


													
//serve-json.js
var http = require('http');
var fs = require('fs');
console.log('Server will listen at :  127.0.0.1:3000 ');
http.createServer(function (req, res) {
	//change the MIME type to 'application/json' 
    res.writeHead(200, {'Content-Type': 'application/json'});
    //Create a JSON
	let json_response = {
		status : 200 , 
		message : 'succssful' , 
		result : [ 'sunday' , 'monday' , 'tuesday' , 'wednesday' ] , 
		code : 2000
	}
	console.log('Server Running');
	res.end( JSON.stringify(json_response) ); 
}).listen(3000);
													
												

Run

  • Now run the snippet using the following command :
    													
    >node serve-json.js