"connect"
module, which is a framework that's going to allow us to easily plug in functionality to our HTTP server.Step 1: Run "npm init"
Step 2 : Insert all required parameter in 'npm init' and create the start page 'index.js'.
Step 3: In package.json , add the dependencies as below
"dependencies": {
"Connect": "*"
}
Step 4 :
var connect=require('connect');
var app=connect()
.use(function(req,res){
.use(function(req,res){
if(req.url == "/hello")
{
console.log("sending plain");
res.end("Hello from app");
}
else if(req.url == "/hello.json")
{
console.log("sending json");
var data = "Hello";
var jsonData = JSON.stringify(data);
res.setHeader('Content-Type', 'application/json');
res.end(jsonData);
}
else if(req.url == "/hello404")
{
console.log("sending 404 status code")
res.statusCode = 404;
res.end("Oops, could not find something");
}
res.end("I am listening");
})
.listen(1111);
.listen(1111);
console.log("listening at port 1111");
Step 5: Run the application by 'node index.js' and try 'http://localhost:1111/hello' in browser
No comments:
Post a Comment