In this lesson, you will be able to understand how you can use node.js to develop your simple HTTP server
Node.js comes with http/https modules which can be used to develop the HTTP server
So to create Node.js
HTTP server you require HTTP module and then you create the server from that module and finally you bind the server to the port to listen
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
const http=require("http") const port=3000 const server = http.createServer(function(request,response){ console.log("Requested URL :"+request.url) response.end('Node.js HTTP Server!') }) server.listen(port, (err) => { if (err) { return console.log('Error', err) } console.log('HTTP Server is listening on '+port) }) |
Next you can start the server with following command in terminal
1 |
$ node index2.js |
Now you can visit localhost:3000
from your browser
Important
In this code you can see the callback function inside the http.createServer
. This callback function will be called every time when you make a request. Inside the callback function you have request
and response
You can write the callback function separately as shown in the below. This method is very easy for you to manage the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
const http=require("http") const port=3000 const httpCallBack=(request,response)=>{ console.log("Requested URL :"+request.url) response.end('Node.js HTTP Server!') } const server = http.createServer(httpCallBack) server.listen(port, (err) => { if (err) { return console.log('Error', err) } console.log('HTTP Server is listening on '+port) }) |
So, How you are going to handle routing here?
We will see how we are going to route following URL
http://localhost:8000
http://localhost:8000/products
http://localhost:8000/product/add
You can see the switch
statement inside the httpCallBack
function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
const http=require("http") const port=3000 const httpCallBack=(request,response)=>{ switch (request.url){ case '/': response.end('Inside Home') break case '/products': response.end('Inside Products') break case '/product/add': response.end('Inside Product add') break } } const server = http.createServer(httpCallBack) server.listen(port, (err) => { if (err) { return console.log('Error', err) } console.log('HTTP Server is listening on '+port) }) |
Form submission in Node.js Server
Now lets look at how we can submit form to this Node.js HTTP server. First create the form http://localhost:3000/product/add
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
const httpCallBack=(request,response)=>{ switch (request.url){ case '/': response.end('Inside Home') break case '/products': response.end('Inside Products') break case '/product/add': var form ="<form method='post'>" form+="<div><label>Product Name </label>" form+="<input type='text' name='key' /></div>" form+="<input type='submit' value='Submit' />" form+="</form>" response.writeHeader(200, {"Content-Type": "text/html"}); response.end(form) break } } |
When you hit the submit button server should identify the request is coming with POST data. We can use the following code for it. We have if(request.method === 'POST')
to identify it. Then we should read the POST data. We can use following code to read data
1 2 3 |
request.on('data', function (data) { body += data; }); |
This is the completed code of the form submission
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
const httpCallBack=(request,response)=>{ switch (request.url){ case '/': response.end('Inside Home') break case '/products': response.end('Inside Products') break case '/product/add': if(request.method === 'POST'){ let body='' request.on('data',function(data) { body += data; }); request.on('end',function(){ response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Posted Value is :'+body); }); }else{ var form ="<form method='post'>" form+="<div><label>Product Name </label>" form+="<input type='text' name='key' /></div>" form+="<input type='submit' value='Submit' />" form+="</form>" response.writeHeader(200, {"Content-Type": "text/html"}); response.end(form) } break } } |
Note
This HTTP module has very low level features so it is very difficult to build complex web application using this module. You have to develop most of the functions by scratch, which needed for complex application
Building from scratch is time consuming task and it needs lots of testing too
Luckily you have good Node.js frameworks for building complex web applications. They are very robust and rich with the features
There are lot of frameworks but these are the most popular ones:
– express
– hapi
– koa
– restify