Introduction to Node.
js and npm
1. Introduction to [Link]
[Link] 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 [Link]
(i) Asynchronous and Event-Driven: [Link] 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, [Link] 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 [Link] and npm
[Link] can be downloaded from the official site: [Link]
Checking Installation :- After installation, check the versions using:
node -v # Check [Link] version
npm -v # Check npm version
4. Introduction to npm (Node Package Manager)
npm is the default package manager for [Link], used to manage
JavaScript libraries and dependencies.
5. Features of npm
(i) Package Management: Helps install, update, and remove [Link]
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 [Link] Application
(i) Initialize a [Link] project:
npm init
This generates a [Link] file.
(ii) Install dependencies:
npm install express
(iii) Writing a Basic Server Using [Link]
Create a file [Link]:
// Import the built-in 'http' module in [Link]
const http = require('http');
// Create an HTTP server that listens for incoming requests
const server = [Link]((req, res) => {
// Set the response status code to 200 (OK) and set the response
header
[Link](200, { 'Content-Type': 'text/plain' });
// Send the response body and end the response
[Link]('Hello, [Link]!');
});
// Start the server and listen on port 3000
[Link](3000, () => {
[Link]('Congratulations ! Server running on port 3000');
});
Explanation of Each Part:
const http = require('http');
This imports [Link]'s built-in http module, which allows you to create
an HTTP server.
[Link]((req, res) => {...})
[Link]() 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.
[Link](200, { 'Content-Type': 'text/plain' });
[Link](200, { ... }) sets the response header.
200 is the HTTP status code (OK).
{ 'Content-Type': 'text/plain' } sets the response type to plain text.
[Link]('Hello, [Link]!');
This sends "Hello, [Link]!" as the response body and ends the
response.
[Link](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 [Link]
(v) Now, visit [Link] in the browser.
8. Important npm Commands
Command Description
npm init Initialize a [Link] 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) [Link] is a JavaScript runtime used for backend development.
(ii) npm is used to manage packages and dependencies in [Link]
projects.
(iii) [Link] 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 [Link] development!
(vii) In [Link], 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
[Link] file.
(viii) Event loop manages the non-blocked I/O operations.