Node.js is a platform (server-side) which is built on google chrome's javascript v8 engine. It is open source , cross platform and is capable of generating real time web applications. It uses event-driven, non-blocking I/O operation model which makes it efficient and light-weight. It is developed by Ryan Dahl with the team of other developers working at joyent in 2009. Its initial release only supports linux. It is written in C,C++ and javascript. It's package ecosystem 'NPM' is the largest ecosystem of open source libraries in the world.
Based on the architecture of your Operating System, you can download the latest version of node.js from the downloads page of node's official website :
Once you are done with the download , open any text editor and start coding. Some examples of text editors are
Atom , Notepad++ , Visual Studio Code , Sublime Text .
All node.js files are stored with the extension ".js" just like javascript.
Let's follow the ritual of programming and start by creating a program which will print hello world
on the console as output. This code-snippet is as simple as it can be.
//file-name : hello-world-in-node.js
console.log('hello world');
>node hello-world-in-node.js
http
module which we can use to create the server. Get the access to the
module by using require
createServer()
method.
createServer()
method takes a callback function as arguments. This callback is executed each time
a request is received. createServer()
method are request
: which contains
all the information related to client's request such as URL, custom headers , client info , etc. response
: which is used to return the data back to the client. response.writeHead
is an inbuilt method which is used to send the console.log()
function. response.end()
is an inbuilt function which is used to tell the server that
the request has been fulfilled.Along with that we can also send one response using this. server.listen()
is an inbuilt method used to bind to the port an start listening for
incoming connections.
//simple-server-in-nodejs.js
var http = require('http');
var host = '127.0.0.1'
var port = 3000
var server = http.createServer((request, response) => {
response.writeHead(200, {"Content-Type": "text/plain"});
console.log("server Working");
response.end('Server Working Success');
});
server.listen(port,host, (error) => {
if (error) {
return console.log('Error occured : ', error );
}
console.log('server is listening on ' + host + ':'+ port);
});
>node simple-server-in-nodejs.js
response.write()
: It is the inbuilt method which is used to send the response.
In the above section we used response.end()
to send the response but we can only send one response using this.
however , we can send as many responses as we want using response.write()
method.
Now Let's see how we can serve a string in an http server in node.js.
//serve-string.js
var http = require('http');
var host = '127.0.0.1'
var port = 3000
var server = http.createServer((request, response) => {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World!");
response.end();
});
server.listen(port,host, (error) => {
if (error) {
return console.log('Error occured : ', error );
}
console.log('server is listening on ' + host + ':'+ port);
});
>node serve-string.js
In the previous section we learned how we can serve a string using node.js but in practical applications
we hardly need to serve the string. In most of the cases , we serve html files via the node.js server.
So let's see how we can serve a html file using node.js
//serve-html.js
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
console.log("Port Number : 3000");
// change the MIME type from 'text/plain' to 'text/html'
res.writeHead(200, {'Content-Type': 'text/html'});
//reading the content file
fs.readFile('index.html', (err, data) => {
//checking for errors
if (err)
throw err;
console.log("Operation Success");
//sending the response
res.end(data);
});
}).listen(3000);
>node serve-html.js
There may be scenarios when the response which we need to send is not a plain text , For REST Api's we usually
have to send JSON response. So , let's see how we can serve JSON in a node.js server
//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);
>node serve-json.js
Now let's make things intersting and try to serve a pdf file using a node.js server
//serve-pdf.js
var http = require('http');
var fs = require('fs');
console.log('Server will listen at : 127.0.0.1:3000 ');
http.createServer( (req, res)=> {
console.log("Port Number : 3000");
// Change the MIME type to application/pdf
res.writeHead(200, {"Content-Type": "application/pdf"});
fs.readFile('index.pdf', (error,data) => {
if(error){
res.json({'status':'error',msg:err});
}else{
res.write(data);
res.end();
}
});
}).listen(3000);
>node serve-pdf.js
These days serving audio is the basic necessity for websites and it's really simple to serve audio using node.js server.
//serve-mp3.js
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
console.log("Port Number : 3000");
// change MIME type to 'audio/mp3'
res.writeHead(200, {'Content-Type': 'audio/mp3'});
fs.exists('audio.mp3',function(exists){
if(exists)
{
var rstream = fs.createReadStream('audio.mp3');
rstream.pipe(res);
}
else
{
res.end("Its a 404");
}
});
}).listen(3000);
>node serve-mp3.js
And we have saved the best for the last. We can also serve videos using a simple nodejs server.
All you have to do is change the MIME type as shown below :
//serve-mp4.js
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
console.log("Port Number : 3000");
// change the MIME type to 'video/mp4'
res.writeHead(200, {'Content-Type': 'video/mp4'});
fs.exists('video.mp4',function(exists){
if(exists)
{
var rstream = fs.createReadStream('video.mp4');
rstream.pipe(res);
}
else
{
res.send("Its a 404");
res.end();
}
});
}).listen(3000);
>node serve-mp4.js
In this part of the node.js series 30 days of node we learned about node.js server
.
Also a basic introduction to node.js and how we can install node.js. Then an hello world
example in node.js.
Then we learned how to create a simple http server in node.js and serve the following :