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
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

Node.js Cheatsheet — Complete Developer Reference

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.


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

CommandPurpose
.helpDisplay help info
.exitExit REPL
.save filename.jsSave REPL session to file
.load filename.jsLoad 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

ModuleDescriptionExample
fsFile system operationsfs.readFile()
httpCreate HTTP serverhttp.createServer()
pathHandle file pathspath.join()
osSystem infoos.platform()
eventsEvent handlingemitter.on()
urlParse URLsurl.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.

Related Article
Machine Learning Cheatsheet (Unsupervised & Reinforcement Learning)

Machine Learning (ML) is a crucial part of artificial intelligence, enabling systems to automatically learn from data.This Machine Learning Cheatsheet Read more

HTML Cheat Sheet — Reference Guide to HTML Tags, Attributes, and Examples

HTML cheat sheet, HTML tags reference, HTML attributes list, HTML examples for beginners, semantic HTML guide, HTML forms tutorial, HTML Read more

Python Cheat Sheet — Complete Syntax Reference and Programming Examples

This Python Cheat Sheet is your quick reference guide for writing efficient Python code. Whether you’re preparing for coding interviews, Read more

PHP Cheat Sheet — Complete Syntax Reference and Programming Examples for Beginners

PHP Cheat Sheet — Complete Syntax Reference & Examples This PHP Cheat Sheet serves as a quick, structured reference for Read more

JavaScript Cheat Sheet — Complete ES6 Syntax, Functions, and DOM Methods with Examples

JavaScript Cheat Sheet — Complete Syntax Reference & Examples JavaScript is the core scripting language of the web, enabling interactivity, Read more

CSS Cheat Sheet — Complete CSS3 Selectors, Properties, and Layout Examples

CSS Cheat Sheet — Complete CSS3 Syntax, Selectors & Layout Examples Cascading Style Sheets (CSS) is the language used to Read more

Java Cheat Sheet — Complete Java Syntax, Data Types, Loops, and OOP Concepts for Beginners

Java Cheat Sheet — Complete Java Syntax, Classes, and Examples Java is a powerful, object-oriented programming language widely used for Read more

HTML5 Cheat Sheet — Complete Tag Reference & Examples

HTML5 is the core markup language of the modern web, used to structure content such as text, images, forms, and Read more

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments