express, app, node

The simplest Express app

Building the simplest Express js app in three lines of code.

The short answer

let app = require('express')()
app.get('/', (req, res) => res.send('Hello world!'))
app.listen(3000, () => console.log('Listening on port 3000'))

The code explanation and a step-by-step walk through:

Create project and install dependencies

In your terminal window, create a project directory:

mkdir simplest-express-app
cd simplest-exrpess-app

Create a package with default settings:

yarn init -y

the -y flag indicates default 'yes' answers to all the questions when creating a package.json file. It's fine for quick a demo or prototyping purposes.

Install express:

yarn add express

The simplest Express app

In your code editor, create a file app.js.

We will start by requiring the express package and at the same time calling a top-level function to create an app instance:

let app = require('express')()

Create a single method for responding to HTTP GET requests at the root of the domain:

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

Create an HTTP server on port 3000:

app.listen(3000, () => console.log('Listening on port 3000'))

That's it. You have just created the simplest Express app.

Test your Express app

In a terminal window, run app.js

node app.js

In a web browser go to [http://localhost:3000] and you should see the message 'Hello world!'

To stop the app, go to your terminal window and press Ctr+C

Powered by

Node
Github
Copyright 2019 - present