Node.js is a powerful JavaScript runtime environment built on Chrome’s V8 engine that allows you to execute JavaScript on the server-side. It’s the backbone of modern web apps, APIs, and scalable microservices.
This Node.js Cheatsheet is designed as a quick and complete reference — ideal for developers learning backend programming or refreshing their knowledge.
Table of Contents:
What is Node.js?
Node.js enables developers to build fast, scalable, and real-time web applications using JavaScript beyond the browser.
It uses a non-blocking, event-driven I/O model, making it ideal for handling multiple requests efficiently.
✅ Key Features of Node.js:
- Built on V8 JavaScript Engine for speed
- Asynchronous and event-driven architecture
- Single-threaded but supports concurrent handling
- Huge ecosystem of packages via NPM
- Ideal for APIs, chat apps, and streaming servers
Installing Node.js
Check Node and NPM Versions
node -v
npm -v
Install a Package
npm install express
Initialize a Project
npm init -y
Node.js REPL Commands
| Command | Purpose |
|---|---|
.help | Display help info |
.exit | Exit REPL |
.save filename.js | Save REPL session to file |
.load filename.js | Load file into REPL |
Creating Your First Node.js Server
// server.js
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello from Node.js Server!');
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000');
});
Run:
node server.js
Node.js Core Modules
| Module | Description | Example |
|---|---|---|
fs | File system operations | fs.readFile() |
http | Create HTTP server | http.createServer() |
path | Handle file paths | path.join() |
os | System info | os.platform() |
events | Event handling | emitter.on() |
url | Parse URLs | url.parse() |
node.js cheatsheet, node.js tutorial, node.js examples, node.js file system, node.js server, express.js tutorial, node.js event loop, node.js asynchronous programming, node.js beginner guide, node.js quick reference
Working with File System (fs)
Read File
const fs = require('fs');
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
Write File
fs.writeFile('output.txt', 'Hello Node!', err => {
if (err) throw err;
console.log('File written successfully');
});
Append Data
fs.appendFile('log.txt', 'New log entry\n', err => {
if (err) throw err;
});
Node.js Event Emitter
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on('message', (msg) => {
console.log(`Received: ${msg}`);
});
emitter.emit('message', 'Hello Event System!');
Node.js Modules and Exports
module1.js
exports.greet = function(name) {
return `Hello, ${name}!`;
};
main.js
const greet = require('./module1');
console.log(greet.greet('Developer'));
Node.js URL Module
const url = require('url');
const myURL = new URL('https://example.com/product?id=100&name=shirt');
console.log(myURL.hostname); // example.com
console.log(myURL.searchParams.get('name')); // shirt
Node.js OS Module
const os = require('os');
console.log(os.platform());
console.log(os.totalmem());
console.log(os.uptime());
Asynchronous Programming in Node.js
Node.js uses callbacks, promises, and async/await for asynchronous operations.
Using Promises
const fs = require('fs').promises;
async function readData() {
const data = await fs.readFile('example.txt', 'utf8');
console.log(data);
}
readData();
Node.js with Express Framework
Install Express:
npm install express
Example server:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Welcome to Express Server');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
Node.js Middleware Example
app.use((req, res, next) => {
console.log(`Request made at: ${new Date().toISOString()}`);
next();
});
Node.js REST API Example
const express = require('express');
const app = express();
app.use(express.json());
let users = [{ id: 1, name: "John" }];
app.get('/users', (req, res) => res.json(users));
app.post('/users', (req, res) => {
users.push(req.body);
res.json({ message: "User added" });
});
app.listen(4000, () => console.log('API running on port 4000'));
Node.js Process Object
console.log(process.pid);
console.log(process.platform);
console.log(process.cwd());
Exit the process:
process.exit();
Node.js Best Practices
✅ Use environment variables with .env
✅ Handle errors using try...catch
✅ Use async/await instead of callbacks
✅ Organize routes and controllers properly
✅ Use a package manager (NPM/Yarn)
FAQ — Node.js Cheatsheet
Q1: What is Node.js used for?
Node.js is used to build fast, scalable, and real-time server-side applications using JavaScript.
Q2: Is Node.js a framework or runtime?
Node.js is a runtime environment, not a framework.
Q3: What makes Node.js asynchronous?
Its event-driven, non-blocking I/O system allows multiple requests to be processed simultaneously.
Q4: Can Node.js work with databases?
Yes, Node.js works with MySQL, MongoDB, PostgreSQL, and Redis seamlessly.
Q5: Is Node.js good for APIs?
Absolutely. Node.js is ideal for RESTful and GraphQL APIs due to its lightweight architecture.

