简体   繁体   English

错误的服务器路径节点 js express

[英]Wrong server paths node js express

I am trying to deploy my node js express app.我正在尝试部署我的节点 js express 应用程序。 But it's getting wrong path.但它走错了路。

app.use("/", express.static(__dirname + "/public")) In server.js And my paths for styles are app.use("/", express.static(__dirname + "/public")) 在 server.js 和我的 styles 的路径是

Directory for style is /public/styles/brandAnimation.css 样式目录是 /public/styles/brandAnimation.css

If your link in your HTML page is:如果您在 HTML 页面中的链接是:

<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.并且, /public是您的服务器文件所在的子文件夹,其中包含app.use("/", express.static(__dirname + "/public"))并且brandAnimation.css位于该/public文件夹中的styles/brandAnimation.cssstyles/brandAnimation.css ,那么这应该工作。

I'd also recommend that you change your express.static() line to just this:我还建议您将您的express.static()行更改为:

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.基本上,您需要 URL 让您的样式表页面以/开头,并且它需要添加到您告诉express.static()要查看的任何目录中。


If this doesn't work for you, then we need these pieces of information:如果这对您不起作用,那么我们需要以下信息:

  1. Your exact stylesheet link from your HTML page您的 HTML 页面上的确切样式表链接
  2. An absolute folder location for your server file that contains the app.use(express.static(...)) statement.包含app.use(express.static(...))语句的服务器文件的绝对文件夹位置。
  3. An absolute folder location and filename for the CSS file you want to be served.您要提供的 CSS 文件的绝对文件夹位置和文件名。

To troubleshoot, add this as the very first middleware in your Express server:要进行故障排除,请将其添加为 Express 服务器中的第一个中间件:

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

This will show what exact requests are arriving at your Express server.这将显示到达您的 Express 服务器的确切请求。 If nothing shows here for the css file, then perhaps your nginx is not configured properly.如果 css 文件在此处没有显示,则可能是您的 nginx 配置不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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