【问题标题】:Serving static files with restify使用 restify 提供静态文件
【发布时间】:2013-10-17 09:07:23
【问题描述】:

我正在学习使用 Node.js。目前,我的文件夹结构如下所示:

index.html
server.js
client
  index.html
  subs
    index.html
    page.html
res
  css
    style.css
  img
    profile.png
  js
    page.js
    jquery.min.js

server.js 是我的网络服务器代码。我使用node server.js 从命令行运行它。该文件的内容是:

var restify = require('restify');

var server = restify.createServer({
    name: 'Test App',
    version: '1.0.0'
});

server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());

server.get('/echo/:name', function (req, res, next) {
    res.send(req.params);
    return next();
});

server.listen(2000, function () {
    console.log('%s running on %s', server.name, server.url);
});

如您所见,此服务器依赖于 RESTIFY。有人告诉我必须使用 RESTIFY。但是,我不知道如何提供静态文件。例如,如何在我的应用程序中提供 *.html、*.css、*.png 和 *.js 文件?

谢谢!

【问题讨论】:

标签: node.js restify


【解决方案1】:

来自documentation

server.get(/\/docs\/public\/?.*/, restify.plugins.serveStatic({
  directory: './public'
}));

但这会搜索./public/docs/public/目录中的文件。
如果您不想将请求路径附加到它,请使用appendRequestPath: false 选项。

我更喜欢在这里使用__dirname 键:

server.get(/\/public\/?.*/, restify.plugins.serveStatic({
    directory: __dirname 
}));

__dirname的值等于脚本文件目录路径,假设也是一个文件夹,这里是public目录。

现在我们将所有/public/.* url 映射到./public/ 目录。


现在还存在serveStaticFiles插件:

server.get('/public/*', // don't forget the `/*`
     restify.plugins.serveStaticFiles('./doc/v1')
); // GET /public/index.html -> ./doc/v1/index.html file

【讨论】:

  • __dirname 在你的情况下会有什么价值?
【解决方案2】:

从 Restify 7 开始,路由 no longer take full regexes,因此如果您希望 /public/stylesheet.css 为文件 ./public/stylesheet.css 提供服务,您的代码现在将如下所示:

server.get('/public/*', restify.plugins.serveStatic({
  directory: __dirname,
}))

这是因为 Restify 7 有一个新的(可能更快的)路由后端:find-my-way

【讨论】:

    【解决方案3】:

    这就是我在 restify 中提供静态文件的方式

    server.get(/\/public\/docs\/?.*/, restify.plugins.serveStatic({
    
              directory: __dirname,
    
              default: 'index.html'
    
            }));
    

    公共访问路径将是:example.com/public/docs/index.html

    【讨论】:

      【解决方案4】:

      试试这个:这里的视图是一个静态资源目录名

      server.get('/\/.*/', restify.plugins.serveStatic({
      
          directory: __dirname + "/view/",
          default: './home.html' 
      
         })
      );
      

      【讨论】:

        【解决方案5】:
        server.get('/', function(req, res, next) {
            fs.readFile(__dirname + '/index.html', function (err, data) {
                if (err) {
                    next(err);
                    return;
                }
                res.setHeader('Content-Type', 'text/html');
                res.writeHead(200);
                res.end(data);
                next();
            });
        });
        

        【讨论】:

        • 如何导入fs?
        【解决方案6】:

        根据我目前的restify版本(v5.2.0)

        serveStatic 已移至plugins,所以代码是这样的

        server.get(
          /\/(.*)?.*/,
          restify.plugins.serveStatic({
            directory: './static',
          })
        )
        

        上面的语法将在文件夹static 上提供您的静态文件。所以你可以得到像http://yoursite.com/awesome-photo.jpg这样的静态文件

        出于某种原因,如果您想在特定路径下提供静态文件,例如 http://yoursite.com/assets/awesome-photo.jpg

        代码应该重构成这样

        server.get(
          /\/assets\/(.*)?.*/,
          restify.plugins.serveStatic({
            directory: `${app_root}/static`,
            appendRequestPath: false
          })
        )
        

        上面的选项appendRequestPath: false 表示我们不在文件名中包含assets 路径

        【讨论】:

          【解决方案7】:

          我最近才遇到这个问题,所以虽然这可能对您没有帮助,但它可以帮助遇到问题的其他人。

          当您将 Restify 声明为 const restify = require('restify'); 时,serveStatic 方法将在插件对象中,因此使用 restify.serveStatic 将悄悄地失败。访问方法的正确方法是restify.plugins.serveStatic

          您可以在此处找到更新文档:http://restify.com/docs/plugins-api/#serve-static

          【讨论】:

            猜你喜欢
            • 2013-03-06
            • 2013-10-19
            • 2013-06-10
            • 1970-01-01
            • 2014-02-11
            • 2020-09-11
            • 2011-01-27
            • 2015-03-04
            相关资源
            最近更新 更多