Home › JavaScript › Node.js
JavaScript
Node.js
Node.js lets you run JavaScript outside the browser, on servers, on the command line, anywhere. The language is the same; the surroundings are different.
Modules and npm
Code is organized into modules you import. npm installs packages other people wrote.
npm init -y
npm install express
A small server
Node ships with everything needed to answer HTTP requests.
import http from 'node:http';
http.createServer((req, res) => {
res.end('Hello from Node');
}).listen(3000);
Working with files
The fs module reads and writes files; most of its functions have a promise version.
import { readFile } from 'node:fs/promises';
const text = await readFile('data.txt', 'utf8');