Node js

Kavinduchamith
4 min readMay 14, 2022

What is Node js?

Node js
Node js

Ryan Dahl created Node JS back in 2009.Node.js is a single-threaded, open-source, cross-platform runtime environment for developing server-side and networking applications that are fast and scalable.

Under the hood of Node.js is a V8 engine.Node.js is completely free. It runs on a variety of platforms. Windows, Linux, Unix, Mac OS X, and other operating systems are examples. On the server, Node.js employs JavaScript.

Features of Node js?

Features of Node js

Node js is the one of most popular technology among the all developers, because it contains some unique features that make node js very fast and efficient. Some of them are,

· Fast and allowing for networking capability.

· Non-blocking thread execution.

· Best for in-browser, real-time, and gaming applications.

· Safe and dependable

· Can implement both frontend and backend using JavaScript.

· High throughput and scalability

Where to use Node JS?

usage of Node js

Because of the asynchronous nature, Node js mostly used for heavy IO intensive applications (Network applications).As I mentioned early Node Js is single threaded, because of that it is not suitable for applications that have heavy CPU intensive tasks.

These are some uses of Node Js

· JSON APIs based Applications

· Data Streaming Applications.

· I/O bound Applications

· Single page application

· Data intensive real-time applications

What are the Advantages of Node JS?

There are many advantages we can gain by using Node js. When I started web developing I used JavaScript for to do some rendering task related to the frontend. In that time we used different technologies for backend and frontend. But with Node js, now we can develop complete application only using JavaScript related technologies.

Because of this reason most of the developers choose Node js over other technologies.

When consider about performance and reusability I think Node js the best technology that we can gain both requirements. If we use some other technology like Java servlet,asp.net the usability and performance is somewhat low because in those applications ,we need to have separate server to run the business logic. But in Node js within about 7 line of codes we can create a server.

These are some other advantages we can gain by using Node js

· Easy to learn

· Offer high performance

· Single Programming language

· Easy Scalability

· Cross-Platform Development

npm (Node Package Manager)

Node Package Manager

npm is the world largest software registry that means it contains over 800 000 code packages. Code packages means when we developing applications using node js we need different kind of services to fulfill our requirements.

In simple term I can mention that npm packages are like group of services that freely available for everyone

npm registry is open-source that means anyone can develop a service and add that service to the npm registry, so all other developers can easily install those services and use in their projects. To use npm packages we need to have node installed.

These are commonly used npm commands

npm init — to initialize the project

npm install <package name> — to install any npm package

All the installed node modules are specified in a file called package.json It is is a file that contains all the information about our project.(metadata)

sample package.json file

Create Simple application server

There are different ways to create server, in this case I am going to demonstrate the server implementation using “express” as a middleware.

First we need to install the express using npm command

npm install express

Then we can import the express to a variable like this

const express = require(‘express’)

Then we can Calls the express function express() and puts new Express application inside the app variable.

const app = express()

Then we need to specify the port number that our server need to run.

const port = 3000

From this code we said to the server that Run this on a GET request, for the given URL.

app.get(‘/’, (req, res) => {res.send(‘Hello Node!’)})

The app. listen() function is used to bind and listen the connections on the specified host and port

app.listen(port, () => {console.log(`server running on port${port}`)})

The server is not ready to run. Then we can run the application by entering following command.

$ node <filename.js>

sample server

Then, load http://localhost:3000/ in any browser to see the output.

I hope you found this useful. I would like to hear from you so feel free to drop a comment or connect with me via LinkedIn.

--

--