简体   繁体   中英

Wrong server paths node js express

I am trying to deploy my node js express app. But it's getting wrong path.

app.use("/", express.static(__dirname + "/public")) In server.js And my paths for styles are

Directory for style is /public/styles/brandAnimation.css

If your link in your HTML page is:

<link rel="stylesheet" href="/styles/brandAnimation.css">

And, /public is a sub-folder from where your server file that contains app.use("/", express.static(__dirname + "/public")) is located and brandAnimation.css is in that /public folder under styles/brandAnimation.css , then this should work.

I'd also recommend that you change your express.static() line to just this:

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

Basically, you need to URL for your stylesheet page to start with a / and it needs to be additive to whatever directory you told express.static() to look into.


If this doesn't work for you, then we need these pieces of information:

  1. Your exact stylesheet link from your HTML page
  2. An absolute folder location for your server file that contains the app.use(express.static(...)) statement.
  3. An absolute folder location and filename for the CSS file you want to be served.

To troubleshoot, add this as the very first middleware in your Express server:

app.use((req, res, next) => {
    console.log(`Incoming request: ${req.path}`);
    next();
});

This will show what exact requests are arriving at your Express server. If nothing shows here for the css file, then perhaps your nginx is not configured properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM