A Simple NodeJS server without Express JS

Now a days, Node JS is blooming in the IT industry. For those who are wondering “ what the heck is Node JS ?” here is the official website of Node JS https://nodejs.org/ and you can easily download and install it in your local machine from https://nodejs.org/en/download/

Now coming to the point, how to create a Node JS server, here is few simple steps to follow :

  1. Create a new folder in your computer using command
mkdir nodeapp

2. go to that folder from terminal using command

cd nodeapp/

3. now run the below command to create “package.json” file in that directory

npm init

press enter enter enter till you see something like below and hit “yes” when prompt

Hey wait.. but what is package.json ?

well it is a kind of manifest for your node application and the npm packages we are going to use in our app.

Note : name key in package.json only take lower case letters, if your folder name contains any uppercase letter you will prompt to write app name in lower case

check your folder if package.json is created or not.

if created, yaay…!! you are good to go.


4. After creating package.json, now it’s time to create node server. For this we have to create a new JS file with any filename you prefer. I prefer “app.js” feel free to neglect my preference :)

touch app.js
touch command will not work on window unless you use git bash

5. open app.js in any text editor, i prefer sublime text. alternate options VS code, Atom, aptana studio etc. and write the following code :

var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); //write a response
res.end(); //end the response
}).listen(3000, function(){
console.log("server start at port 3000"); //the server object listens on port 3000
});

That’s it…. !!!!

here “http” is the built in module of Node JS through which we can transfer data over HTTP protocols. here is the list of all the built-in modules of Node JS https://nodejs.org/dist/latest-v8.x/docs/api/

now to run your node server run the command in your terminal

node app.js

you will see “server start at port 3000” message in your terminal. now go to http://localhost:3000/ you will see

Congratulation…!! you just create a node JS server :)

to exit server hit Ctrl+c

if you want response displayed as HTML, include http header and start server

var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'}); // http header
res.write('<h1>Hello World!<h1>'); //write a response
res.end(); //end the response
}).listen(3000, function(){
console.log("server start at port 3000"); //the server object listens on port 3000
});

before proceeding any further, take a minute break to understand what you have done till now.


Have you notice that if you hit any other localhost urls apart from http://localhost:3000/ it is also showing “hello world”. e.g: http://localhost:3000/about , http://localhost:3000/contact . Naah.. this is not what you want, right..??

let’s add some routing to our app.js

var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'}); // http header
var url = req.url;
if(url ==='/about'){
res.write('<h1>about us page<h1>'); //write a response
res.end(); //end the response
}else if(url ==='/contact'){
res.write('<h1>contact us page<h1>'); //write a response
res.end(); //end the response
}else{
res.write('<h1>Hello World!<h1>'); //write a response
res.end(); //end the response
}
}).listen(3000, function(){
console.log("server start at port 3000"); //the server object listens on port 3000
});

Now again start server and check

http://localhost:3000/,

http://localhost:3000/about ,

http://localhost:3000/contact

I have put the code in github also. here is the link of the repo https://github.com/SK-CSE/nodeapp