vignesh waran vignesh waran - 4 months ago 48x
Node.js Question

Express js is not serving static files

I am new to express js.I think this is a newbie js is not serving static files

var express = require('express');
.set('view engine','ejs')

Project Structure


I think you need to use:

.use('/public', express.static('public'))

The first public is what you use as a prefix in your url. The second public is your physical folder in your file system.

Another option would be not to use /public/* in your urls.

From the docs:

To create a virtual path prefix (where the path does not actually exist in the file system) for files that are served by the express.static function, specify a mount path for the static directory, as shown below:

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

Now, you can load the files that are in the public directory from the /static path prefix.