Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Unlike JavaScript in the browser, which is mainly used for building web user interfaces, Node.js is used for building scalable network applications, especially server-side applications.
1. Why Use Node.js?
2. Installation:
You can download the installer from the official website: https://nodejs.org/
3. First Node.js Script:
Once installed, you can write your first Node.js script.
// hello.js
console.log("Hello, Node.js!");
Run the script by using the following command:
node hello.js
You should see the output: Hello, Node.js!
4. Creating a Basic HTTP Server:
Here’s a simple example of creating a server that responds with 'Hello, World!'.
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, World!\n');
});
server.listen(3000, () => {
console.log('Server running at http://127.0.0.1:3000/');
});
Run this script with node server.js, and visit http://127.0.0.1:3000/ in your browser.
5. NPM - Node Package Manager:
It's a tool that comes with Node.js and allows you to install, share, and manage libraries (packages) in your projects.
Example: Installing the popular request library:
npm install request
6. Create a Basic Web Application Using Express.js:
Express is a minimal and flexible Node.js web application framework, providing a robust set of features for building web apps and APIs.
Installation:
npm install express
Sample Express app:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, Express.js!');
});
app.listen(3000, () => {
console.log('App listening on port 3000!');
});
Run the app and visit http://127.0.0.1:3000/.
7. Asynchronous Nature of Node.js:
In Node.js, operations are non-blocking, meaning they don’t stop the execution of other code. Here's a simple example:
const fs = require('fs');
// Non-blocking code
fs.readFile('file.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
console.log('Reading file...');
Even if reading the file takes time, the "Reading file..." message will be displayed immediately.
8. Handling Data:
Node.js makes it easy to handle data, be it form data, JSON, or other formats.
For instance, using Express:
const express = require('express');
const app = express();
app.use(express.json()); // for parsing application/json
app.post('/data', (req, res) => {
console.log(req.body);
res.send('Data received');
});
app.listen(3000);
9. Connecting to Databases:
Many libraries, like mongoose for MongoDB, are available via NPM to connect with databases.
10. Deployment:
Once your application is ready, you can deploy it to various hosting platforms like Heroku, AWS, or DigitalOcean.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here