NodeJs is an open source cross-platform, javascript runtime environment for servers and environments. Chrome's V8 JavaScript are building blocks which parses and executes JavaScript code. NodeJs is a widely used programming model.
NodeJs |
- Download the Latest (LTS- Long Term Support) Version of Node from NodeJs.
- Download Visual Studio Code (VS Code) as an Editor (recommended) from VS Code.
Create new folder for your project naming project name. Open the folder in VS Code. Then, open a new terminal and type the command:
When only npm init command is applied,
you have to fillup every information about the node project like the package name, version, description, author, license, keyword, etc. details. Entering every step takes default values. However, npm init -y auto fills all information needed setting to default values at once. If you like you could edit them on package.json file formed after this command. This creates a package.json file. This file is the main file of any Node project. It's hold metadata of the project that defines functional attributes to install dependencies and to run script.
How to write your first program (Hello World) in NodeJs?
Now, execute the file from the terminal using the command: either node hello.js or node hello without .js file extension.
The result is shown right below the command. It prints the string statement given inside the method console.log().
How to create a Server in Node?
Node Server can be created using 'http' core module of Node. The core module of Node need not be required to install in every project as they already came up with Node. Only importing the module works totally fine. Some important core modules are fs, http, path, querystring, url, util, etc.
Create new file index.js (index file) in the project folder using the '+' sign on top-left side of VS Code Window. This appears when you hover over the project name.
In the index file, we import http module using require method. To import the package, write; const http = require('http'); line of code in line 1. createServer method of http is used to create a server. Listening on an unused port. The port number can be saved as a variable but for ease, for now, we simply give the port value to listen to method.
index.js
Now, execute the file using node index.js from the terminal to run the web server. when the web server works it would print out 'Server is running at port 3000' i.e. same string you provided in the log method. Visit localhost:3000 to see the response 'Hello World from Node Server' same as you set in res.end method in index.js.
How to create Node Server using ExpressJs?
Express is a third-party package/module built over NodeJs thus, need to install it before using it in the project. ExpressJs is NodeJs web application framework that helps in the smooth development of the server-based applications. Here, we are going to create a basic web server in NodeJs using ExpressJs.
Node application depends on various modules and packages, that we install and import before using them in the application. Installation is required for third-party packages only. We here install each required package once it's needed! The first essential package is express. So, in the terminal of VS Code in the project directory we download express using the code below.
This will create a new folder in the project directory 'node_modules' that contains all the packages you install and their dependencies packages. For now, you get sub-folders of express and its dependencies.
In the index file we remove all previous content, then we import express using require method. To import the package, write; const express = require('express'); line of code in line 1. Few line of codes is sufficient to create a server. It's the most efficient way to create a server using express.
index.js
To run the web server in the terminal execute the file node index.js and visit http://localhost:3000 you get the response 'Home page' in web-page and 'Server is started and listening at port 3000' in VS Code terminal right after the command: node index.js.
0 Comments