Creating a Basic Express App

·

1 min read

Creating a Basic Express App

Photo by Joan Gamell on Unsplash

Table of contents

No heading

No headings in the article.

This app starts a server and listens on port 3000 for connections. The app responds with “Hello World!” for requests to the root URL (/) or route. For every other path, it will respond with a 404 Not Found.

Requirements to run this app:

  • Install the express module using > npm install express --save.

  • Have a text editor. I'm using VS Code.🙂

const express = require('express')
const app = express()

const port = normalise(process.env.PORT || '3000')
app.set('port', port)

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

app.listen(app.get('port'), () => {
  console.log(`Example app listening on port ${app.get('port')}`)
})

Run the app with the following command:

node app.js

Then open your browser and go to localhost:3000