0% found this document useful (0 votes)
14 views8 pages

Nodejs

Node.js is a JavaScript runtime that enables server-side execution of JavaScript, allowing for the development of scalable applications. It features a non-blocking I/O model, a large ecosystem via npm, and is suitable for real-time applications. The document also covers installation, creating applications, using npm, modular programming, and file operations in Node.js.

Uploaded by

Sekhar Muthangi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

Nodejs

Node.js is a JavaScript runtime that enables server-side execution of JavaScript, allowing for the development of scalable applications. It features a non-blocking I/O model, a large ecosystem via npm, and is suitable for real-time applications. The document also covers installation, creating applications, using npm, modular programming, and file operations in Node.js.

Uploaded by

Sekhar Muthangi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Node.

js: A Comprehensive Guide

What is [Link]?

[Link] is a JavaScript runtime built on Chrome's V8 JavaScript engine. It allows developers to


execute JavaScript code on the server side, enabling the creation of scalable and high-performance
applications.

 [Link] is an open source server environment

 [Link] is free

 [Link] runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)

 [Link] uses JavaScript on the server

Why Use [Link]?

1. Non-blocking I/O model: Ideal for handling concurrent requests efficiently.

2. Single programming language: JavaScript can be used for both frontend and backend.

3. Large ecosystem: Thousands of libraries are available via npm (Node Package Manager).

4. Fast execution: Built on V8, it’s optimized for speed.

5. Scalability: Excellent for real-time applications like chat apps and streaming services.

Why [Link]?

[Link] uses asynchronous programming!

A common task for a web server can be to open a file on the server and return the content to the
client.

Here is how PHP or ASP handles a file request:

1. Sends the task to the computer's file system.

2. Waits while the file system opens and reads the file.

3. Returns the content to the client.

4. Ready to handle the next request.

Here is how [Link] handles a file request:

1. Sends the task to the computer's file system.

2. Ready to handle the next request.

3. When the file system has opened and read the file, the server returns the content to the
client.
[Link] eliminates the waiting, and simply continues with the next request.

[Link] runs single-threaded, non-blocking, asynchronous programming, which is very memory


efficient.

What Can [Link] Do?

 [Link] can generate dynamic page content

 [Link] can create, open, read, write, delete, and close files on the server

 [Link] can collect form data

 [Link] can add, delete, modify data in your database

What is a [Link] File?

 [Link] files contain tasks that will be executed on certain events

 A typical event is someone trying to access a port on the server

 [Link] files must be initiated on the server before having any effect

 [Link] files have extension ".js"

Use Cases for [Link]

 Real-time chat applications

 APIs and backend services

 Server-side rendering

 Streaming data applications

Setting Up and Using [Link]

Installation

1. Download [Link] from [Link] official website.

2. Install it by following the platform-specific instructions.

3. Verify installation:

4. node -v

5. npm -v

Creating a Simple [Link] Application

1. Create a file named [Link].


2. Add the following code:

3. [Link]("Hello, [Link]!");

4. Run the file:

5. node [Link]

Creating a Web Server in [Link]

[Link] provides a built-in http module to create web servers.

Example: Simple Web Server

const http = require('http');

const server = [Link]((req, res) => {

[Link](200, { 'Content-Type': 'text/plain' });

[Link]('Hello, World!');

});

[Link](3000, () => {

[Link]('Server running at [Link]

});

 Explanation:

o [Link]: Creates the server.

o [Link]: Sets the HTTP status and headers.

o [Link]: Sends the response and ends the request.

Run the server:

node [Link]

Visit [Link] in your browser.

Node Package Manager (npm)

What is npm?

npm is the default package manager for [Link]. It helps manage libraries and dependencies.

Common Commands

 Initialize a project:
 npm init -y

 Install a package:

 npm install <package-name>

 Uninstall a package:

 npm uninstall <package-name>

 Global installation:

 npm install -g <package-name>

Example: Using npm to Add Express

1. Install Express:

2. npm install express

3. Example usage:

4. const express = require('express');

5. const app = express();

6.

7. [Link]('/', (req, res) => {

8. [Link]('Hello, Express!');

9. });

10.

11. [Link](3000, () => {

12. [Link]('Express server running at [Link]

13. });

Modular Programming in [Link]

What is Modular Programming?

It involves breaking down the application into smaller, reusable modules for better maintainability
and readability.

Example: Creating and Using a Module

Create a file [Link]:

1. function add(a, b) {

2. return a + b;

3. }
4. function subtract(a, b) {

5. return a - b;

6. }

7. [Link] = { add, subtract };

Use the module in [Link]:

8. const math = require('./math');

9. [Link]([Link](5, 3)); // Output: 8

10. [Link]([Link](5, 3)); // Output: 2

Restarting [Link] Applications

Why Restart Automatically?

We generally type following command for starting NodeJs server:

node [Link]

In this case, if we make any changes to the project then we will have to restart the server by killing
it using CTRL+C and then typing the same command again.

node [Link]

It is a very hectic task for the development process.

Nodemon is a package for handling this restart process automatically when changes occur in the
project file.

Installing nodemon: nodemon should be installed globally in our system:

Windows system: npm i nodemon -g

Linux system: sudo npm i nodemon -g


Now, let’s check that nodemon has been installed properly to the system by typing the following
command in terminal or command prompt:

nodemon -v

It will show the version of nodemon as shown in the below screenshot.

Starting node server with nodemon:

nodemon [Your node application]

Now, when we make changes to our nodejs application, the server automatically restarts by
nodemon as shown in the below screenshot.

In this way with nodemon server automatically restarts.


Installing and Using nodemon

1. Install nodemon globally:

2. npm install -g nodemon

3. Run your application with nodemon:

4. nodemon [Link]

File Operations in [Link]

Built-in fs Module

[Link] provides the fs module to handle file operations.

Example: Reading and Writing Files

const fs = require('fs');

// Write to a file

[Link]('[Link]', 'Hello, [Link]!', (err) => {

if (err) throw err;

[Link]('File written successfully!');

// Read the file

[Link]('[Link]', 'utf8', (err, data) => {

if (err) throw err;

[Link]('File content:', data);

});

});

Other File Operations

Append to a file:

 [Link]('[Link]', '\nAppended content.', (err) => {

 if (err) throw err;

 [Link]('Content appended!');

 });

Delete a file:

 [Link]('[Link]', (err) => {

 if (err) throw err;


 [Link]('File deleted!');

 });

You might also like