NodeJS is widely used for building scalable and high-performance applications, particularly for server-side development. It is commonly employed for web servers, APIs, real-time applications, and microservices.
- Perfect for handling concurrent requests due to its non-blocking I/O model.
- Used in building RESTful APIs, real-time applications like chats, and more.
Creating Console-based Node Application
1. Running NodeJS with Console (No Setup Required)
NodeJS allows developers to run JavaScript directly in the console without any setup. We can start the NodeJS REPL (Read-Eval-Print Loop) by simply typing node in the terminal and running JavaScript commands interactively.
$ node
> let name = "Jiya";
> console.log("Hello, " + name + "!");
Output
Node First ApplicationCreating Application with npm init and Installed Modules
Step 1: Initialize a NodeJS Project
mkdir my-node-app
cd my-node-app
npm init -y
Step 2: Install Required Modules
We will install fs (for handling file operations) and path (for working with file paths).
npm install fs path
Step 3: Create an index.js File
Create a simple HTTP server that reads and serves a file.
JavaScript
import http from "http";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const server = http.createServer((req, res) => {
const filePath = path.join(__dirname, "message.txt");
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("Error reading file");
} else {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end(data);
}
});
});
server.listen(3000, () => {
console.log("Server is running on port 3000");
});
Step 4: Create a message.txt File
Create a message.txt file in the same directory and add some content:
JavaScript
Hello, this is a Node.js application without Express!
Step 5: Run the Application
node index.js
Creating Web-based Node Application
A web-based Node application consists of the following three important components:
- Import required module
- Create server
- Read Request and return response
Let us learn more about them in detail
Step 1: Import required modules
Load Node modules using the required directive. Load the http module and store the returned HTTP instance into a variable.
Syntax:
var http = require("http");Step 2: Creating a server in Node
Create a server to listen to the client's requests. Create a server instance using the createServer() method. Bind the server to port 8080 using the listen method associated with the server instance.
Syntax:
http.createServer().listen(8080);
Step 3: Read request and return response in Node
Read the client request made using the browser or console and return the response. A function with request and response parameters is used to read client requests and return responses.
Syntax:
http.createServer(function (request, response) {...}).listen(8080);step 4: Create an index.js File
JavaScript
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
Step 5: To run the program type the following command in terminal
node index.js
Output:


Core Components of a Node-First Application
- Backend (NodeJS with Express.js/NestJS/Koa.js): The backend serves as the core processing unit, handling API requests, database interactions, and authentication mechanisms.
- Database (MongoDB, PostgreSQL, MySQL, Redis): A Node-First approach commonly utilizes NoSQL databases like MongoDB for scalability, though SQL databases like PostgreSQL or MySQL are also widely used.
- Frontend (React, Vue.js, Angular): Although frontend technologies can vary, JavaScript frameworks like React.js, Vue.js, and Angular work seamlessly with a Node-powered backend.
- Authentication & Security: Utilizing JWT (JSON Web Tokens), OAuth, or session-based authentication ensures secure user interactions.
- Real-Time Capabilities (Socket.io, WebRTC): For real-time applications like chat apps or live updates, Socket.io provides seamless WebSocket communication.
What is the purpose of the Node.js REPL?
-
To compile JavaScript into machine code
-
To interactively execute JavaScript commands in the terminal
-
To deploy Node.js applications
-
Explanation:
The REPL (Read-Eval-Print Loop) allows developers to run JavaScript directly in the console without creating files.
Which command initializes a new Node.js project by creating a package.json file?
Explanation:
npm init -y creates a package.json with default values, allowing the project to start quickly.
Which module is used to create a basic HTTP server in Node.js?
Explanation:
The http module provides built-in tools to create servers without external frameworks.
What does the following code do?
res.writeHead(200, { "Content-Type": "text/html" });
-
Writes HTML code to a file
-
Sends a success status and sets response headers
-
-
Starts listening on a port
Explanation:
writeHead(200) sends the HTTP status code 200 (OK) and specifies the type of content being returned.
Quiz Completed Successfully
Your Score : 2/4
Accuracy : 0%
Login to View Explanation
1/4
1/4
< Previous
Next >
Explore
Introduction & Installation
Node.js Modules , Buffer & Streams
Node.js Asynchronous Programming
Node.js NPM
Node.js Deployments & Communication
Resources & Tools