What is Node.js?
Node.js lets you run JavaScript outside the browser to build servers and tools.
Syntax
node app.jsNode.js is a runtime that lets you run JavaScript on your computer or a server, not just inside a web browser. It was created in 2009 and is built on Google's fast V8 JavaScript engine.
What is it used for?
- Web servers and REST APIs.
- Command-line tools and scripts.
- Real-time apps like chat and games.
- Build tools for front-end projects.
Because Node uses JavaScript, you can write both the front end and the back end of an app in one language.
These lessons show real Node.js code. The examples are not run in your browser (Node runs on a server), but every snippet is correct and idiomatic — copy it into a file and run it with node.Example
// hello.js — run it with: node hello.js
console.log('Hello from Node.js!');
console.log('Node version:', process.version);When to use it
- A startup builds its REST API in Node.js so the same JavaScript developers who wrote the React frontend can own the backend too.
- A DevOps team writes a Node.js CLI tool to automate deployments instead of maintaining separate shell scripts.
- A gaming company uses Node.js to handle thousands of simultaneous WebSocket connections for a real-time multiplayer lobby.
More examples
Hello World in Node
The simplest Node.js program — runs with `node hello.js` and prints to stdout.
// hello.js
console.log('Hello from Node.js!');
console.log('Node version:', process.version);Simple HTTP server
Creates a minimal web server that responds to every request — no framework needed.
const http = require('http');
const server = http.createServer((req, res) => {
res.end('Hello from Node HTTP server!\n');
});
server.listen(3000);Read a file with Node
Uses the built-in `fs` module to read a file asynchronously, illustrating Node's I/O capabilities.
const fs = require('fs');
fs.readFile('readme.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
Discussion