top of page

A Hello, World! Node.js Server on Windows 10


Node.js logo

This post shows how to write, start, and stop a Node.js HTTP server that prints Hello, World!


A Hello, World! Node.js Server on Windows 10


1. Put this into a file called server.js (or click here to download one).

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {  
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

2. Open the folder and right-click on Open in Terminal


Open the folder containing server.js and click Open in Terminal

3. Type node .\server.js


4. Ctrl-click on the link or open a browser and type http://127.0.0.1:3000/ or click here

See Hello, World! in a browser

5. Type Ctrl-c to quit


Other


Node.js logo clipped from https://nodejs.org/


The version posted at https://nodejs.org/en

// server.mjs
import { createServer } from 'node:http';
const server = createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World!\n');
});
// starts a simple http server locally on port 3000
server.listen(3000, '127.0.0.1', () => {
  console.log('Listening on 127.0.0.1:3000');
});
// run with `node server.mjs`

Comments


bottom of page