Jul 1, 2024
node --version
node app.js
command.module.exports
to expose functions or variables outside the module.require
to load other modules.fs
, http
, os
, path
, etc.const path = require('path');
const os = require('os');
const fs = require('fs');
eventEmitter.on
to handle events and eventEmitter.emit
to raise events.EventEmitter
to create custom event-driven systems.Building a Web Server:
http.createServer
to create a basic web server.request
and response
objects to handle HTTP requests and responses.const http = require('http');
const server = http.createServer((req, res) => {
if (req.url === '/') {
res.write('Hello World');
res.end();
} else if (req.url === '/api/courses') {
res.write(JSON.stringify([1, 2, 3]));
res.end();
}
});
server.listen(3000);
console.log('Listening on port 3000...');