Minimal Node.js Express Static File Server

Build a minimal static file server with Express.

Setup

npm init
npm install express --save

Server

Create server.js:

// server.js
const express = require('express');
const app = express();

app.use(express.static('www'))

/* routes root '/' to index.html */
app.get('/', (req, res) => {
    res.sendFile('/index.html');
})

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

All files in www folder are now served automatically.

Static Content

Create www/index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Static express</title>
  </head>
  <body>
    <div>
        <p>All work and no play makes Jack a dull boy.</p>
    </div>
  </body>
</html>

Run

node ./server.js

Open localhost:3000 in your browser.

Docker

Create Dockerfile:

## Dockerfile
FROM node
WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install

COPY . .
EXPOSE 3000
CMD ["node", "server.js"]

Create .dockerignore:

node_modules
npm-debug.log

Build and run:

docker build -t express-static-server .
docker run -p 3000:3000 express-static-server

Open localhost:3000 in your browser.