Hello World in Node.js
Time to write real programs. We will build three "hello worlds" of increasing usefulness: a console log, a script with logic, and an actual HTTP server — the thing that makes Node.js a backend platform. By the end you will understand not just how to run each, but why the server keeps running while the script exits.
1. The classic console log
hello.js
console.log('Hello, World!')node hello.js
Hello, World!
2. A script that does something
greet.js
const hour = new Date().getHours()
const greeting =
hour < 12 ? 'Good morning' : hour < 18 ? 'Good afternoon' : 'Good evening'
const name = process.argv[2] || 'World'
console.log(`${greeting}, ${name}!`)node greet.js Sam
Good afternoon, Sam!
3. Your first web server
This is the moment Node.js becomes a server. Using the built-in http module, you can answer real HTTP requests in a handful of lines — no framework, no install:
server.js
import http from 'node:http'
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Hello, World from a Node.js server!\n')
})
const PORT = 3000
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`)
})node server.js
Server running at http://localhost:3000
Open http://localhost:3000 in a browser — you will see your message. Or test it from another terminal with curl:
curl http://localhost:3000
Hello, World from a Node.js server!
Press Ctrl + C in the terminal to stop the server.
Why does the server keep running?
The request handler runs per request
The function you passed to createServer is a callback invoked once for every incoming request, with a fresh req (readable stream of the request) and res (writable stream for your response). Because it all runs on the single thread, the golden rule applies: keep the handler fast and never block it.
What you just learned
Concept | Where it showed up |
|---|---|
Running files |
|
Reading input |
|
Core modules |
|
Script vs server lifetime | The loop exits when empty, stays alive with a listener |
Request/response streams | The |