This website works best with JavaScript enabled.
Bookmark
⚡️ CLI & Project Setup
node -v
npm -v
npm init -y
npm install express
node app.js
npm run start
📦 Modules
// CommonJS
const fs = require('fs');
const path = require('path');

// ES Modules
import fs from 'fs';
import path from 'path';
🏗️ File System
const fs = require('fs');

fs.readFile('file.txt', 'utf8', (err, data) => { /* ... */ });
fs.writeFile('file.txt', 'Hello', err => { /* ... */ });
fs.appendFile('file.txt', 'World', err => { /* ... */ });
fs.unlink('file.txt', err => { /* ... */ });
🌐 HTTP Server
const http = require('http');
const server = http.createServer((req, res) => {
  res.end('Hello Node');
});
server.listen(3000);
🚀 Express
const express = require('express');
const app = express();

app.get('/', (req, res) => res.send('Home'));
app.post('/data', (req, res) => res.json({ ok: true }));

app.listen(3000);
🛡️ Environment Variables
// .env
PORT=3000

// app.js
require('dotenv').config();
console.log(process.env.PORT);
🗂️ Path & OS
const path = require('path');
const os = require('os');

console.log(path.join(__dirname, 'file.txt'));
console.log(os.platform(), os.cpus());
🔄 Process & Events
process.argv        // Command line args
process.env         // Env variables
process.exit(0)     // Exit process

const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on('event', () => {});
emitter.emit('event');
🛠️ Misc
console.log('Log');
setTimeout(() => {}, 1000);
setInterval(() => {}, 1000);
Next
Nuxt

Want to update?

Subscribe to my blog to receive the latest updates from us.

Monthly articles
New articles will be created every month.
No spam
All notifications are notified to you, not spam or advertising.

© Tran Nguyen Thuong Truong 2024 - PRESENT