node.js - 如何使用 Restify 提供静态文件

标签 node.js file static restify

我正在使用 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/

相关文章:

javascript - 如何在 Electron 浏览器窗口中查看 PDF?

c - 使用函数从文件打印结构中的值(在 c 中)

java - 是否可以将 Eclipse 配置为在没有静态导入的情况下使用 "favorites"?

c# - 静态类的静态方法如何知道调用者?

node.js - <slot> Angular 5 中的功能

javascript - 错误 : Evaluation failed: ReferenceError: res is not defined

node.js - 如何在Windows Azure中将公共(public)端口80连接到VM端口8080

php - 在 PHP 中查找多个 TXT 文件中的特定文本

c - 从文件读入字符数组

java - Java静态成员和C++静态成员的区别