Serving Static Files in Express
$ ls -l public/ total 48 -rw-r--r-- 1 ubuntu ubuntu 1666 Mar 12 14:17 home.css -rw-r--r--@ 1 ubuntu ubuntu 17092 Mar 12 14:17 logo.png $
You can use the
express.static middleware to make it possible to access files from this folder via HTTP.
const express = require('express'); const app = express(); app.use(express.static('./public')); app.listen(3000);
With the above script, you can open
http://localhost:3000/home.css in your browser and see the CSS file.
Serving HTML Files
static middleware is how you can use Express to serve static HTML files. If you have a vanilla HTML file
test.html, you can open that file in your browser and the browser will render the HTML.
This means that you can use