a blog for those who code

Wednesday 29 March 2017

Create your First Application in Node.js

In this post we will be discussing about creating your first application or simply say first server in Node.js. Node.js is an open source, cross-platform run-time environment for developing server-side web applications. The main idea of Node.js is to use non-blocking, event-driven I/O to remain lightweight and efficient to build real-time applications which will run across distributed devices.

Before creating the application you should be familiar with importing external modules in your application. Modules are loadable JavaScript packages that provide specific functionality for your application. Node.js has a simple module loading system where you need to load the external module using the require directive.

1. Using in-built http module to create Node.js Server

Both http and https module are the built-in modules which are shipped together with Node.js. We will be using this in built http module to create our first Node.js Server as shown below

// Importing http module
var http = require('http');
//creating server
var server = http.createServer((req,res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World');

//console to log message
console.log('Server running at http://localhost:3000');

In the above code we have created an instance of http module and called the createServer() method to create a server. The listen method is used to bind the server to a port (i.e. 3000). We also have a function with request and response parameter. We have then used the response parameter to send the status and the content-type to the client which will access this server along with the message to be displayed on the client screen. Next we are logging the message in our console that the server is running on http://localhost:3000.

2. Using http-server module to create Node.js Server

http-server is a simple, zero-configuration command-line http server. You need to install it via npm using the below command which will install http-server globally so that it may run from the command line.

npm install http-server -g

To start the server type http-server -o which will open the browser after creating the server.

3. Using Express.js to create Node.js Server

Express is a minimal and flexible Node.js web application framework that provides a robust set of features to develop web and mobile applications. Before creating the Node.js server using Express.js you need to install Express Framework which can be done by referring Getting Started with Express - Installation and Setup.

To get started with the basic project in Express you need to have a variable of "Express" libraries and a variable of application on top of your app.js as shown below.

var express = require('express');
var app = express();

Next we need to set up the some of the app environments as shown in the below code.The first app setting defines the port where your application will communicate, the second app settings tells your app where to find its views and the third app settings defines the engine which will render those views.

app.set('port', process.env.port || 5000); 
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

The next step is to creating a basic command to display some text on the web browser as shown below.

app.get('/', function(req, res) {
  res.send('Hello World Express');

Then we need a server to listen to requests pending for your application as shown below.

var server = app.listen(5000, function () {
  console.log('Listening on port ' + server.address().port)

Once done you will run the application by node app.js. If everything is right you can see that the server is listening on port 5000 and when you open http://localhost:5000 in the browser you can see the text you have written earlier.

Please Like and Share the CodingDefined.com Blog, if you find it interesting and helpful.

No comments:

Post a Comment