📝 Edit page
➕ Add page
Static server
Configure Express to serve static assets
From Static files in the docs.
Serve a static directory. e.g. serve the public
directory.
app.use(express.static('public'))
To serve on another path e.g. server contents of public
as a /static
.
app.use('/static', express.static('public'))
Using an absolute path is safer:
const path = require('path')
app.use('/static', express.static(path.join(__dirname, 'public')))
A fuller example:
index.js
import express from "express";
const PORT = 3000
const app = express();
app.use(express.static('public'));
app.use(express.static('images'));
app.listen(PORT);
Or
app.listen(PORT, () => console.log(`Example app listening at http://localhost:${port}/`));