使用Restify提供静态文件服务

33

我正在学习使用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是我的Web服务器代码。我使用node server.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文件?

谢谢!


可能是重复的问题:https://dev59.com/vGUp5IYBdhLWcg3wLVKn?rq=1 - vinaut
7个回答

46

根据文档

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 会有什么值? - letmaik

10
根据我的当前的 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路径包含在文件名中。


8
从Restify 7开始,路由不再接受完整的正则表达式,所以如果你想让/public/stylesheet.css提供文件./public/stylesheet.css,你的代码现在应该是这样的:
server.get('/public/*', restify.plugins.serveStatic({
  directory: __dirname,
}))

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


2
这是我在Restify中提供静态文件的方法。
server.get(/\/public\/docs\/?.*/, restify.plugins.serveStatic({

          directory: __dirname,

          default: 'index.html'

        }));

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

1
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? - Niko

1
我最近遇到了这个问题,所以虽然这可能对你没有帮助,但它可能会帮助其他遇到麻烦的人。
当你将Restify声明为const restify = require('restify');时,serveStatic方法将在plugins对象中,因此使用restify.serveStatic将静默失败。访问该方法的正确方式是restify.plugins.serveStatic
您可以在此处找到更新文档:http://restify.com/docs/plugins-api/#serve-static

0
尝试这个:这里的view是一个静态资源目录名称。
server.get('/\/.*/', restify.plugins.serveStatic({

    directory: __dirname + "/view/",
    default: './home.html' 

   })
);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接