Roadmap › Node.js — Runtime Internals, Express, APIs and SQL Server › Module: Node.js Fundamentals
Lessons in this course (11)
3. Core modules — fs, path, http, events, buffer and process
Basic★ Must knowLesson 3 of 11
js
import http from 'node:http';
import { EventEmitter } from 'node:events';
import path from 'node:path';
// Minimal HTTP server without frameworks
const server = http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/health') {
res.writeHead(200, { 'Content-Type': 'application/json' });
return res.end(JSON.stringify({ status: 'ok' }));
}
res.writeHead(404).end();
});
server.listen(process.env.PORT ?? 3000, () => console.log('listening'));
// EventEmitter — the pattern behind streams, servers and sockets
class OrderEvents extends EventEmitter {}
const events = new OrderEvents();
events.on('placed', (order) => console.log('send email for', order.id));
events.emit('placed', { id: 1001 });
// path and process
const file = path.join(import.meta.dirname, 'data', 'orders.json');
console.log(process.env.NODE_ENV, process.argv, process.memoryUsage().heapUsed);
process.on('SIGTERM', () => server.close(() => process.exit(0))); // graceful shutdown
// Buffer — raw binary data
const buf = Buffer.from('₹100', 'utf8');
console.log(buf.length, buf.toString('base64'));