JavaScript Node.js Express Application

Prerequisites

Initializing Project

Create project directory

mkdir myapp
cd myapp

Init Node.js project

npm init

Follow on-screen prompts. Enter to accept defaults except entry point (index.js), enter app.js instead.

Install express and save in the dependencies list

npm install express --save

Creating Hello World

Create or edit app.js with the following content

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

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

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

Run locally

node app.js

The app should serve at http://localhost:3000 

image-1597384411246.png

References