简体   繁体   English

如何使用Restify提供静态文件

[英]How to serve static file using Restify

I am working with a node application, where I need to serve static files(js,css). 我正在使用节点应用程序,需要在其中提供静态文件(js,css)。

My project structure is like below: 我的项目结构如下:

-Projectroot
     -restifyServer
         -server.js
     -frontEnd
         -index.html
         -js
         -partials
         -css

server.js is not only the server it serves XML also. server.js不仅是它还提供XML的服务器。

Also for serving index.html I have routing like below: 另外,为了提供index.html,我的路由如下:

server.get('/', getIndexhtml);

And getIndexhtml callback is like: 而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();
});
}

When I start server and navigate my browser to ' http://localhost:8080/ ' it loads html without static files. 当我启动服务器并将浏览器导航到“ http://localhost:8080/ ”时,它将加载没有静态文件的html。

In console I get error like below: 在控制台中,出现如下错误:

http://localhost:8080/js/library/jquery.js 404 (Not Found)

How can I configure resitfy to serve static files. 如何配置resitfy以提供静态文件。 I have tried this solution but couldn't make it work. 我已经尝试过解决方案,但无法使其正常工作。

Thanks in advance. 提前致谢。

You didn't serve other static files such as: "js/library/jquery.js". 您没有提供其他静态文件,例如:“ js / library / jquery.js”。 I highly recommend using express framework instead , it's easy for serving static files without doing it manually. 我强烈建议您使用Express Framework代替它,因为无需手动操作即可轻松提供静态文件。

What about having an Apache server for your static site? 为您的静态站点安装Apache服务器呢? That way you won't have to write all through restify. 这样一来,您就不必通过Restify编写全部内容。

Just have your restify server to serve the xml you need (the way you are doing it), and have all your client-side html, css, js files in apache. 只需让您的Restify服务器来提供您所需的xml(您的操作方式),并将所有客户端html,css,js文件保存在apache中即可。

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

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