0

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

1 Answers1

0

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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Still not working or i am doing something wrong. Is it possible to be something from nginx ? – Emil Zlatinov Jan 12 '22 at 19:25
  • @EmilZlatinov - See the troubleshooting step I added to the end of my answer. IAdd that, restart your server, load the web page and report back here what shows in the console. Also, since it isn't working, please supply the three pieces of information my answer asks for. – jfriend00 Jan 12 '22 at 19:28
  • It's saying "incoming requst: / " But nothing showing up... – Emil Zlatinov Jan 12 '22 at 22:14
  • Well, `incoming request: /` does not have anything to do with your style sheet, so perhaps the problem is before it even gets to the stylesheet. Can you show us all the relevant express code, including all middleware and the request handler for whatever web page you're loading and the HTML for that web page? – jfriend00 Jan 12 '22 at 23:15
  • And, you mention nginx. What are you doing with nginx? – jfriend00 Jan 12 '22 at 23:26
  • It was from nginx, something with 404. Everything was fine, and thanks brother! – Emil Zlatinov Jan 13 '22 at 19:08