Home > Net >  Basic Node and Express: Serve Static Assets with file directory
Basic Node and Express: Serve Static Assets with file directory

Time:09-24

let express = require('express');



let app = express();
app.use(express.static('public'));

I am trying to solve freecodecamp's "Basic Node and Express: Serve Static Assets" challenge but it keep says "not working. Could someone help me? Thanks.

This is the path folder The main files in my directory

CodePudding user response:

Use:

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

CodePudding user response:

As described in express documentation:

If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve:

const path = require('path')
app.use('/static', express.static(path.join(__dirname, 'public')))
  • Related