NodeJS Notes
NodeJS Notes
NodeJS
1. Introduction to NodeJS
Overview: NodeJS is a JavaScript runtime built on Chrome's V8 JavaScript engine,
allowing developers to run JavaScript on the server side.
Key Features:
1. Event-driven architecture
2. Non-blocking I/O operations
3. Single-threaded but highly scalable due to the event loop
npm init -y
npm install express --save
// Importing a module
const fs = require('fs');
// Using a module
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
4. Building a Web Server with NodeJS
HTTP Module: Fundamental for creating HTTP servers.
switch (extname) {
case '.js': contentType = 'text/javascript'; break;
case '.css': contentType = 'text/css'; break;
// Add more types as needed
}
Reading Files:
Writing Files:
readFile('file.txt', 'utf8')
.then(data => console.log(data))
.catch(err => console.error(err));
8. Introduction to APIs
A REST API (Representational State Transfer Application Programming Interface) is a
set of rules and conventions for building and interacting with web services. It is based
on the principles of REST, an architectural style for designing networked applications.
REST APIs are widely used for enabling communication between clients (e.g., web
browsers, mobile apps) and servers over the internet.
server.listen(8080, () => {
console.log('Server running on port 8080');
});
Module 7: ExpressJS (NodeJS Framework)
1. Introduction to Express
Express: A Nodejs framework that simplifies server creation, routing, and middleware
handling.
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
3. Routing in Express
Types of Middleware
1. Application level middleware
2. Router-Level Middleware
3. Route-Specific Middleware
4. Error-Handling Middleware:
5. Third-Party Middleware: