How to create a basic server in NodeJs

Reading Time: 2 minutes

What is NodeJs?

Node.js is a JavaScript runtime that is based on Chrome’s V8 engine. It is a server-side JavaScript language. It is open-source and cross-platform.

It’s important to note that NodeJS is not a framework or a programming language. It’s a runtime environment for JavaScript code execution. We frequently utilize it to create back-end services such as APIs, Web Apps, and Mobile Apps.

It also comes with a large library of JavaScript modules, which greatly facilitates the building of Node.js web applications.

Features

  1. Asynchronous and Event-Driven:  The Node.js library’s APIs are all asynchronous, i.e. non-blocking. It basically means that a Node.js-based server never waits for data from an API.
  2. Very Fast:  It’s library is extremely quick in code execution because it is based on Google Chrome’s V8 JavaScript Engine. 
  3. No Buffering:  Data is never buffered in these apps. The data is simply output in chunks by these apps.
  4. It’s simple to use and used for both prototyping as well as in agile development.
  5. Because it uses JavaScript everywhere, it’s simple for a JavaScript programmer to use Node.js to create back-end services.
  6. It offers services that are both quick and scalable.
  7. Cleaner and more consistent codebase.

Advantages of NodeJs

  1. Used to build scalable web applications
  2. Easy to learn
  3. Growing community
  4. Helps to create highly performant applications
  5. Advantage of caching

Disadvantages of NodeJs

  1. Library support system is not strong
  2. API is not stable
Pros and Cons of Nodejs

When to use NodeJs?

NodeJS should be used to create:

  1. Application based on JSON APIs
  2. Data streaming applications
  3. Real time chat applications

Creating the NodeJs server

For creating the NodeJs server, first we need to install it using the given command:

sudo apt install nodejs

Create a file named app.js containing the following contents:

Example of node server

Now run the server using

node app.js. 

Visit http://localhost:3000 and you will see a message saying ” Hello Node from Node! “

Conclusion

Node.js is very useful for building scalable and high-performing online applications. It has also broadened the scope of JavaScript applications, making it suitable for both frontend and backend servers.

Thank you for reading, if you really like the blog hit the like button, and to learn more, visit here

Written by 

Aanchal Agarwal is a Software Consultant at Knoldus. Her practice area is web development. She is recognized as a multi-talented, multitasker, and adaptive to the different work environments. Her hobbies include watching movies, listening to music, and traveling. She likes to read books and explore new things.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading