No comments yet

How to create a web server using Node JS

[Social9_Share type="horizontal"]

2. How to create a webserver using Node JS

In this post , lets learn how to create a webserver using node. If you have not installed Node please click here to install node .Now, the next step is to understand about each concept of Node and trying some practical programs on the same. Let us learn

Node Core Modules

Node comes with built-in core modules for many types of network and file IO. These include the modules for HTTP, TLS, HTTPS, File system, Datagram and NET
To include a module within a Node Application, use the require statement. The identifier for the module is passed as a String.

Example

Here http is an identifier for the core module HTTP in Node JS.
You can load any core module that is native to Node or modules from node_modules folder, by passing the module identifier within the require function.

Points to be noted

• Module files can have either .js or .node extension
• Each file has its own module
• The module name is a string of characters or may have forward slashes for path
• Variables are private to the module
• To import a module, use the global require function
• To export any function outside the module, use module.exports. Now let’s jump into coding.

Program to check the OS

This is a simple program to check the os in which the application is running. For this, create a JavaScript file with the below code and save it as ‘one.js’.

Here, we are importing the ‘os’ module. Then, we are printing the os type and its version in which the application is running. To run this file open command prompt, move to the path where ‘one.js’ is present. Then type, node one.js

Now, you will see the output in the command line.

nodeos

Program to create a web server using Node

This program is to show how to create a webserver using Node. First, import the module http using require function.

Then, call createServer() method on http which listens to port number 3000.

Lets now add a log statement. The process loops behind the scene and continues to do the other operations which are on the way till a request happens.

This will print the message inside console.log() method to command line. Since there is no response to be written, it will print ERR_EMPTY_RESPONSE in the browser.The output in the command line is

nodeserver

Now, let’s add a callback function inside createServer() method.

When you run the program, ‘Server listening to port 3000’ is first printed in the console. When a request happens, then the callback function(request,response) is fired and prints Welcome to Node as response in the browser. This function is triggered, with an onclick event in the browser.

To specify the MIME -type of the response in the header, use the writeHead() method in the call back function.The response will be written to the browser, once the response.end() method is called.The console.log() method inside the callback function will be printed last

The complete program is given below.

The same program can be modified and written as below. Here,the callback function is called on a ‘request’ inside the on method.

The output in the browser is shown below

nodebrowser

Happy Learning.

Post a comment