Open In App

Node First Application

Last Updated : 27 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

console2

Node First Application

Creating 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:

first node application creatednode application hello world output

Why Choose a Node-First Approach?

  • Scalability: NodeJS is known for its ability to handle high concurrent connections efficiently, making it an excellent choice for real-time applications and APIs.
  • Performance Optimization: With its asynchronous, event-driven nature, NodeJS reduces processing latency and improves response times compared to traditional multi-threaded environments.
  • Unified Language for Full-Stack Development: A Node-First approach enables developers to use JavaScript across the stack, reducing context switching and enhancing productivity.
  • Rich Ecosystem and Community Support: The Node Package Manager (NPM) provides access to thousands of pre-built modules, making it easier to integrate functionalities and improve development efficiency.
  • Microservices Compatibility: NodeJS seamlessly integrates with microservices architectures, allowing developers to build modular and independently deployable services.

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.

Best Practices for a Node-First Application

  • Follow Modular Code Structure: Break the application into reusable modules for better maintainability.
  • Use Asynchronous Programming: Leverage async/await or Promises to handle asynchronous tasks efficiently.
  • Implement Proper Logging and Monitoring: Use tools like Winston, Bunyan, or ELK Stack (Elasticsearch, Logstash, Kibana) for logging and monitoring.
  • Secure APIs with Rate Limiting: Prevent abuse by implementing rate-limiting strategies using express-rate-limit.
  • Optimize Performance with Load Balancing: Distribute traffic efficiently using NGINX, HAProxy, or Load Balancers.

Conclusion

A Node-First Application enables developers to build high-performance, scalable, and efficient applications using the power of NodeJS. By leveraging its asynchronous nature, rich ecosystem, and JavaScript’s versatility, developers can create robust applications that cater to modern web development needs.



Next Article

Similar Reads