Introduction to Node.Js [NodeJs] :
- NodeJs is an open source server-side environment to develop back-end of a web service, it is free.
- NodeJs uses JavaScript programming over the server side implementation.
- It runs on various platforms/operating systems such as linux, windows, unix, MacOs, etc.
Why use Node.Js for server side?
- Node.Js uses asynchronous programming means it can handle a request while it sends a response to the client.
- It is memory efficient because it is single-threaded, non-blocking and runs asynchronously.
- It does not let the user to wait and continues with the next request.
Uses of Node.Js:
- Node.Js can generate dynamic page content.
- It can do all the basic operations files on the server.
- Node.Js can also collect form data.
- It can add, modify and delete data in a database.
Installation [NodeJs]:
-
- To install Node.Js on windows go to its official website nodejs.org – click here and you can choose the version and architecture of the application and can download it.
- To install Node.Js in ubuntu you download it by following commandsFor stable version of 8.x
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - sudo apt-get install -y nodejs
- For alternatives of Node.Js 10.x
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - sudo apt-get install -y nodejs
Implementing Scripts:
- Node.Js files contain the tasks that are to be carried after an event is fired from the client.
- An event in Node.Js is that someone trying to access a port on a server.
- It’s files have an extension “.js” as for JavaScript.
- It’s files are to be run on the command line interface of the system and should instantiated before they are executed.
For example create a Node.Js file with the following code and name it as server.js:
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end('Hello World!'); }).listen(8080);
Run the program in the command line interface by using following command:
node server.js
Output: