Local files with relative paths are not loading in NodeJS Express app Local files with relative paths are not loading in NodeJS Express app express express

Local files with relative paths are not loading in NodeJS Express app


express.static middleware is based on serve-static, and is responsible for serving the static assets of an Express application.

How it works:

  • Serve static content for the app from the "public" directory in the application directory

    // GET /style.css etc

    app.use(express.static(__dirname + '/public'));

  • Mount the middleware at "/static" to serve static content only when their request path is prefixed with "/static"

    // GET /static/style.css etc.

    app.use('/static', express.static(__dirname + '/public'));

  • Serve static files from multiple directories, but give precedence to "./public" over the others

    app.use(express.static(__dirname + '/public'));

    app.use(express.static(__dirname + '/files'));

    app.use(express.static(__dirname + '/uploads'));