简体   繁体   English

在Node.js中提供静态文件-500内部服务器错误

[英]Serving static files in node.js - 500 internal server error

I'm having a weird issue where our staging server is throwing a 500 error when trying to retrieve css or js assets. 我遇到一个奇怪的问题,我们的登台服务器在尝试检索CSS或JS资产时抛出500错误。 We are using broccoli to compile the assets to a distribution directory, so I have ~/dist/assets/app.css (as well as app.js and an img directory). 我们正在使用西兰花将资产编译到分发目录,因此我具有〜/ dist / assets / app.css(以及app.js和img目录)。 Images seem to be served fine! 图片似乎可以很好地投放! Only the app.js and app.css files are throwing the 500 error. 仅app.js和app.css文件抛出500错误。 I've ensured the files definitely exist in their proper places. 我确保文件肯定存在于它们的适当位置。 We're using express.js and the serve-static module. 我们正在使用express.js和serve-static模块。 Code is simply: 代码很简单:

serveStatic = require('serve-static');
app.use(serveStatic('dist/assets'));

Then hitting ' http://url.com/assets/app.css ' throws the 500. Hitting ' http://url.com/app.css ' WORKS. 然后点击“ http://url.com/assets/app.css ”会抛出500。点击“ http://url.com/app.css ”即可。 This seems like it would be okay (since I'm serving dist/assets so the request should be relative to /assets), but this was all working with the /assets prefix on the request a few days ago. 这似乎没问题(因为我正在提供dist / assets,因此请求应该相对于/ assets),但这几天前都在请求上使用了/ assets前缀。 There is no error output produced in the logs anywhere. 在任何地方的日志中都不会产生错误输出。 Stumped on this one. 困在这一个。 I just want to make sure I'm not doing anything too dumb. 我只是想确保我没有做任何愚蠢的事情。

Have you tried this: 您是否尝试过:

serveStatic = require('serve-static');
app.use(serveStatic('dist'));

serveStatic(root, options) serveStatic(root,选项)

Create a new middleware function to serve files from within a given root directory. 创建一个新的中间件功能,以从给定的根目录中提供文件。

Based on that statement, you should expect that using "serveStatic('dist/assets')" will serve the app.css from http://url.com/app.css 根据该声明,您应该期望使用“ serveStatic('dist / assets')”可以从http://url.com/app.css提供app.css

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

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