Hello World Server

Write and run your first Express server that responds to a browser request.

Syntaxapp.METHOD(path, handler)

Here is the classic first Express program. Save it as app.js and run it with node app.js. Then open http://localhost:3000 in your browser.

What each line does

  • require('express') loads the library.
  • express() creates an application object, usually called app.
  • app.get('/', handler) registers a route for GET /.
  • app.listen(3000) starts the server on port 3000.

The handler receives two objects: req (the incoming request) and res (the response you send back).

Example

Example · javascript
const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello World!');
});

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

When to use it

  • A bootcamp instructor walks students through writing a Hello World server to verify their Node.js and Express setup is working.
  • A developer uses a minimal Hello World server as a health-check endpoint to confirm a new VM or container has outbound network access.
  • A team starts every new microservice with a Hello World scaffold, then progressively layers in routes and middleware.

More examples

Classic Hello World server

The canonical first Express program: one route responds to GET / with plain text and logs the URL once ready.

Example · js
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

app.listen(3000, () => {
  console.log('Server running at http://localhost:3000');
});

Port from environment variable

Reads the port from the environment so the same server code works in development and on cloud platforms like Heroku.

Example · js
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => res.send('Hello, World!'));

app.listen(PORT, () => console.log(`Listening on port ${PORT}`));

Hello World with JSON response

Returns a JSON object instead of plain text, matching the style of a real API health or welcome endpoint.

Example · js
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.json({ message: 'Hello, World!', timestamp: new Date().toISOString() });
});

app.listen(3000);

Discussion

  • Be the first to comment on this lesson.