Simplest node express static files server

The simplest node static files server.

Create the project and install express package

npm init
npm install express --save

Create server.js file in the root.

// 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..'));

Create a static page

Now all files in www folder are being served. Create a www folder in the root and add an html file.

<!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 it locally

node ./server.js

Open a browser at localhost:3000.

Now docker, of course.

Create a Dockerfile

# Dockerfile
FROM node
WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install

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

And a .dockerignore

node_modules
npm-debug.log

Run it

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

Open a browser at localhost:3000.