我正在尝试使用Restify提供静态文件,但没有成功。我在网上搜索了包括restify站点,到目前为止我找到的解决方案都没有解决我得到的“资源未找到”的错误。这是我的代码-
server.get(/.*/, restify.serveStatic({
'directory': 'public',
'default': 'test.csv'
}));发布于 2015-09-26 02:05:37
首先,你需要看看你的文件夹结构。restify将您在使用serveStatic构建文件路径时指定的目录和路由路径组合在一起。
假设您的项目位于/project/test中
包含使用server.listen()启动服务器的模块的名为app.js的文件位于/project/test/lib中
并且您将公共内容放置在/project/test/public中
所以现在你有了一个结构,看起来像
/lib app.js
/public test.csv
问题是,如果您在app.js中添加像这样的静态路由
server.get(/\/public\/?.*/, restify.serveStatic({
'directory': __dirname,
'default': 'test.csv'
}));__dirname变量返回/project/test/lib
因此,restify正在为/project/test/lib/public中的http://domain.com/public寻找没有内容的内容
您需要将/public移到/lib中才能正常工作
如果你想保持相同的文件夹结构,你需要像这样做
server.get(/\/public\/?.*/, restify.serveStatic({
'directory': __dirname.replace('/lib', ''),
'default': 'test.csv'
}));因此,directory的值现在为/project/test,路由将在/project/test/public中查找静态内容。
发布于 2015-08-15 03:17:15
如果你想从URL中的公共路径请求资源,你需要这样设置你的路由:
server.get(/\/public\/?.*/, restify.serveStatic({
'directory': 'public',
'default': 'test.csv'
}));而且您必须将文件放在应用程序根目录的public目录中。
https://stackoverflow.com/questions/32017180
复制相似问题