24

我有以下代码:

应用程序.js

[...]

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

server.listen(1337, function() {
  console.log('%s listening at %s', server.name, server.url);
});

我有以下文件结构

app.js
public/
  index.html

所以我尝试浏览:

http://localhost:1337/docs/public/index.html

我得到

{
  code: "ResourceNotFound",
  message: "/docs/public/index.html"
}

我尝试了几种变体,但它们似乎都不起作用。

我敢肯定这应该是很明显的我想念的东西

4

3 回答 3

20

restify将使用该directory选项作为整个路由路径的前缀。在您的情况下,它将寻找./public/docs/public/index.html.

于 2013-03-17T17:53:50.510 回答
8
  1. directory选项是整个路径的前缀。
  2. 相对路径在更高版本的 Restify 中无法正常工作(我测试了 2.6.0-3、2.8.2-3 - 它们都产生 NotAuthorized 错误)

现在解决方案变为:

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

然后你的静态文件将需要在./docs/public.
__dirname是一个全局变量,包含您正在运行的脚本的绝对路径)

于 2014-11-22T09:59:11.107 回答
6

根据@NdeeJim 的回答,任何想知道如何提供所有静态资源的人:

server.get(/\/?.*/, restify.plugins.serveStatic({
            directory: __dirname,
            default: 'index.html',
            match: /^((?!app.js).)*$/   // we should deny access to the application source
     }));
于 2015-09-30T22:04:12.983 回答