Getting started with NodeJs



Step1 (Installation):

The first step is to install nodeJS from its official website nodejs.org

After installation check your installation by typing the below command in command prompt

node --version

Step 2 (Start server):

The next step is to create a local server in your machine.  Create a new file named 'server.js' (Note: You can name the file as whatever name you want).  Open the server.js file in a text editor and type the below code in it.

var http = require('http');
var port = 5000;

http.createServer ((reqres=> {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end("Hello, World!");
}).listen(port);

 Don't forget to save the file.  Now open the file in command prompt (Note: Use 'cd' command to navigate through folders).  In command prompt execute the below command to start the local server.

node server

After executing the command nothing will happen if there are no errors (Note: Don't close the terminal because the local server is running through command prompt).  Open browser and type localhost:5000.  Your output will appear like the below image in your browser.


Congratulations!, now your local server is up and running successfully.

Comments