Node.js fundamentals
Node.js is a runtime that executes JavaScript outside the browser. It has no DOM. Instead, it provides APIs for files, processes, streams, networking and operating-system interaction.
console.log('Hello from Node.js');
console.log(process.version);
console.log(process.cwd());Files and modules
ES modules make relationships explicit. Node’s built-in modules use the node: prefix.
import { readFile } from 'node:fs/promises';
const text = await readFile('./notes.txt', 'utf8');
console.log(text);Environment variables
Configuration that changes between environments can come from process.env. Never expose secrets to browser bundles or commit them to Git.
const port = process.env.PORT || 3000;The request-response model
A server listens for a request, validates its method and data, performs work and returns one response with a status, headers and body.
import { createServer } from 'node:http';
createServer((request, response) => {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ message: 'Hello' }));
}).listen(3000);Building APIs with Express
Express is a separate library built on Node’s HTTP capabilities. It supplies routing and middleware conventions; Node.js and Express are not inseparable.
import express from 'express';
const app = express();
app.use(express.json());
app.post('/api/lessons', (request, response) => {
const title = String(request.body.title || '').trim();
if (!title) return response.status(400).json({ error: 'Title required' });
response.status(201).json({ title });
});
app.listen(3000);Real applications also need structured validation, error handling, tests, authorization, rate limits and secure database access.
Express needs a compatible runtime
Express does not run continuously on static hosts such as ESA Pages. Deploy Express to a compatible server, container or function platform. On Alibaba Cloud, that may be ECS, SAE, a container service or a suitably configured Function Compute application.
Databases and authentication are a later stage: learn data modelling, password hashing, sessions or tokens, authorization and security review before handling real user accounts.