In this tutorial we will see how express serves static files. Express module has a middleware function static() which instructs express to treat a folder as static. Let us take a look at the below example.
Example – Express Serve Static File
Before we begin, create a folder at the project root and name it “files”. Now create another folder and name it “images”. After folder creation, place an image inside the images folder. Your folder structure should look like below.
var express = require("express"); var app = express(); app.use(express.static('files')); app.listen(8080);
Make a note that the folder “files” is not part of the url.