0% found this document useful (0 votes)
4 views4 pages

Introduction to Node.js and Npm

Node.js is an open-source JavaScript runtime environment for server-side applications, known for its asynchronous, event-driven architecture and fast execution. npm, the default package manager for Node.js, helps manage libraries and dependencies, allowing for easy installation and version control. A basic Node.js application can be created by initializing a project, installing dependencies, and setting up an HTTP server using the built-in 'http' module.

Uploaded by

ahanacherry08
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)
4 views4 pages

Introduction to Node.js and Npm

Node.js is an open-source JavaScript runtime environment for server-side applications, known for its asynchronous, event-driven architecture and fast execution. npm, the default package manager for Node.js, helps manage libraries and dependencies, allowing for easy installation and version control. A basic Node.js application can be created by initializing a project, installing dependencies, and setting up an HTTP server using the built-in 'http' module.

Uploaded by

ahanacherry08
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
You are on page 1/ 4

Introduction to Node.

js and npm
1. Introduction to Node.js
Node.js is an open-source, cross-platform JavaScript runtime
environment used for developing server-side applications. It is built on
the Chrome V8 JavaScript Engine and was developed by Ryan Dahl in
2009.

2. Features of Node.js
(i) Asynchronous and Event-Driven: Node.js uses a non-blocking I/O
model.
All operations run in the background, improving performance.
(ii) Single-Threaded with Event Loop: Unlike traditional multi-threaded
models, Node.js follows a single-threaded model with event looping.
Note : The event loop ensures that tasks are executed in the correct
order, enabling asynchronous programming.
(iii) Fast Execution: Built on the V8 engine, which compiles JavaScript to
machine code for fast execution.
(iv) Highly Scalable: Handles multiple requests efficiently using event-
driven architecture.
(v) Cross-Platform Compatibility: Works on Windows, macOS, and Linux.

3. Installing Node.js and npm


Node.js can be downloaded from the official site: https://nodejs.org
Checking Installation :- After installation, check the versions using:
node -v # Check Node.js version
npm -v # Check npm version

4. Introduction to npm (Node Package Manager)


npm is the default package manager for Node.js, used to manage
JavaScript libraries and dependencies.

5. Features of npm
(i) Package Management: Helps install, update, and remove Node.js
packages.
(ii) Version Control: Allows managing different versions of
dependencies.
(iii) Global & Local Installation:
Local Installation: Package is installed in the project directory.
Global Installation: Package is available system-wide.
6. Installing a Package Using npm
To install a package (e.g., Express):

npm install express


For a global installation:
npm install -g nodemon
To uninstall a package:
npm uninstall express

7. Creating a Simple Node.js Application


(i) Initialize a Node.js project:
npm init
This generates a package.json file.
(ii) Install dependencies:
npm install express
(iii) Writing a Basic Server Using Node.js
Create a file server.js:
// Import the built-in 'http' module in Node.js
const http = require('http');

// Create an HTTP server that listens for incoming requests


const server = http.createServer((req, res) => {

// Set the response status code to 200 (OK) and set the response
header
res.writeHead(200, { 'Content-Type': 'text/plain' });

// Send the response body and end the response


res.end('Hello, Node.js!');
});

// Start the server and listen on port 3000


server.listen(3000, () => {
console.log('Congratulations ! Server running on port 3000');
});

Explanation of Each Part:


const http = require('http');
This imports Node.js's built-in http module, which allows you to create
an HTTP server.
http.createServer((req, res) => {...})
http.createServer() creates a new server.
It takes a callback function that is executed whenever a request (req) is
received.

The res object is used to send a response back to the client.


res.writeHead(200, { 'Content-Type': 'text/plain' });
res.writeHead(200, { ... }) sets the response header.
200 is the HTTP status code (OK).
{ 'Content-Type': 'text/plain' } sets the response type to plain text.
res.end('Hello, Node.js!');
This sends "Hello, Node.js!" as the response body and ends the
response.
server.listen(3000, () => {...})

This starts the server and makes it listen for requests on port 3000.

The callback function runs once the server starts successfully, logging a
confirmation message.
(iv) Run the server:
node server.js
(v) Now, visit http://localhost:3000 in the browser.

8. Important npm Commands


Command Description
npm init Initialize a Node.js project
npm install <package> Install a package locally
npm install -g <package> Install a package globally
npm uninstall <package> Remove a package
npm update Update all packages
npm list List installed packages

9. Summary
(i) Node.js is a JavaScript runtime used for backend development.
(ii) npm is used to manage packages and dependencies in Node.js
projects.
(iii) Node.js provides a fast, scalable, and event-driven architecture.
(iv) Basic server setup is done using the http module.
(vi) These concepts form the foundation of Node.js development!
(vii) In Node.js, dependencies refer to external libraries or modules that
a project requires to function properly. These dependencies are typically
installed using npm (Node Package Manager) and are listed in the
package.json file.
(viii) Event loop manages the non-blocked I/O operations.

You might also like