Skip to content
tezvyn:
Menu

Minimal Express Hello World server

Source: interviewEasyHow cards are made

Summary

basic Express setup fluency.

Key points

import express, create an app, define app.get on the root sending a response, call app.listen on a port.

Watch out for

forgetting app.listen or confusing the require with the app instance.

What's really being asked

This verifies baseline Express familiarity, the starting point for every Express discussion. It quickly reveals whether someone has actually built with the framework.

The full answer

You require the express module, which exports a factory function, and call it to create an application instance, conventionally named app. You define a route by calling app.get with the path '/' and a handler function receiving the request and response objects, where you call res.send('Hello, World!') to send the body; Express automatically sets a 200 status and an appropriate Content-Type. Finally you call app.listen with the port number and usually a callback that logs that the server is running. That is the entire minimal program. Express handles the underlying http server creation, the response finalization, and content-type detection for you, which is why this is shorter than the equivalent raw http module code.

The mistakes people make

Forgetting to invoke express() and trying to call get on the module itself. Omitting app.listen, so the routes exist but nothing binds a port. Using the raw http module patterns like manually calling res.end with headers when Express provides res.send.

What usually comes next

How does res.send differ from res.json and res.end. How would you add middleware. How do you read query and route parameters in the handler.

A concrete example

The full program: const express = require('express'); const app = express(); app.get('/', (req, res) => res.send('Hello, World!')); app.listen(3000, () => console.log('Server on 3000'));. A GET to http://localhost:3000/ returns Hello, World! with a 200 status, and the listen callback confirms the server bound to the port.

Interview question

In a minimal Express app, what does omitting the app.listen call result in?

  • a.The server never binds to a port, so no requests are servedCorrect
  • b.Express throws an error at startup demanding a port
  • c.Routes respond but on a random default port
  • d.The app serves only the root route and ignores others
Why?

app.listen binds the server to a port; without it nothing accepts connections. Express does not pick a random port, throw automatically, or selectively serve routes.

Just read this? Test yourself on what you have been reading.

Read the original → expressjs.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on express — each one lists the topics its interview covers.

See open roles