DEV Community

Cover image for Understanding Basics of Node.js
suyash200
suyash200

Posted on

Understanding Basics of Node.js

Introduction

Node.js is a javascript runtime environment built on V8 engine. It is open-source, single threaded and is used to make server-side applications.

Lets understand by visualization how node.js works
An visual overview of how node.js works

Modules in Node

Core feature of node is modules. Modules are simple or complex
task that are pre-build in them. Each module in Node.js has its own context, so it cannot interfere with other modules or pollute global scope. It is based on common Javascript modules standard
Modules are of three types:

  • Core: In built modules that are pre installed with node.js

  • Local: Local modules are modules created locally in your Node.js application.

  • Third-Party: External packages that are installed via package managers like npm or yarn.

Node package Manager(NPM)

"There is a npm package for that."
This quote sums up importance of npm. It allows third party packages to be installed to your project. Anyone can upload their
packages to website and allow others to use.

Example

Now that we take a look at the basics of node.js lets write a program that covers http module in node used for backend development.

var http = require('http');

http.createServer(function (req, res) {
  res.write('Hello World!');  
  res.end();  
}).listen(8080); 
Enter fullscreen mode Exit fullscreen mode

In above code at line 1 http module is imported as it is a Core module in node.js.
createserver is used to create a server
res,req represents request and response of the server
.listen() represents on which port the requests are going to be heard.
The output of the following server would be on port 8000:

Output of the above server

This has been basics of node.js. More documentation can always be found on node docs.Thank you for reading

Top comments (0)