我正在使用 Node 应用程序,我需要在其中提供静态文件(js,css)。
我的项目结构如下:
-Projectroot
-restifyServer
-server.js
-frontEnd
-index.html
-js
-partials
-css
server.js 不仅是服务器,它还提供 XML 服务。
同样为了提供index.html,我有如下路由:
server.get('/', getIndexhtml);
getIndexhtml 回调如下:
var getIndexhtml = function indexHTML(req, res, next) {
fs.readFile(__dirname + '/../frontendapp/index.html', function (err, data) {
if (err) {
next(err);
return;
}
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.end(data);
next();
});
}
当我启动服务器并将浏览器导航到“http://localhost:8080/
”时,它会加载没有静态文件的 html。
在控制台中我收到如下错误:
http://localhost:8080/js/library/jquery.js 404 (Not Found)
如何配置 resitfy 来提供静态文件。我已经尝试过this解决方案,但无法使其工作。
提前致谢。
最佳答案
您没有提供其他静态文件,例如:“js/library/jquery.js”。我强烈建议使用express framework相反,无需手动操作即可轻松提供静态文件。
关于node.js - 如何使用 Restify 提供静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17094113/