serving static files with restify (node.js)

点点圈 提交于 2019-12-02 21:17:38

restify will use the directory option as a prefix for the entire route path. In your case, it will look for ./public/docs/public/index.html.

  1. The directory option is a prefix for your entire path.
  2. Relative paths are not working correctly in later versions of Restify (I tested 2.6.0-3, 2.8.2-3 - and they all produce the NotAuthorized error)

The solution now becomes:

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

And then your static files will need to be in ./docs/public.
(__dirname is a global variable that contains the absolute path of the script you are running)

phosphore

Based on @NdeeJim's answer, to anyone wondering how to serve ALL the static resources:

server.get(/\/?.*/, restify.plugins.serveStatic({
            directory: __dirname,
            default: 'index.html',
            match: /^((?!app.js).)*$/   // we should deny access to the application source
     }));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!