【发布时间】:2013-09-22 18:08:42
【问题描述】:
这就是我得到的。它工作得很好,但我希望能够在客户登录我的网站时向客户发送文件和数据(JSON)。有没有办法把它结合起来?
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
【问题讨论】:
标签: javascript node.js express
这就是我得到的。它工作得很好,但我希望能够在客户登录我的网站时向客户发送文件和数据(JSON)。有没有办法把它结合起来?
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
【问题讨论】:
标签: javascript node.js express
您不能一次发送 2 个文件。但是您可以使用带有ejs 的模板库将JSON 嵌入到html 中。
【讨论】:
流只能为请求发送一种类型的内容。但是,根据您的 Accept 标头,您可以为同一请求 URL 上的不同请求发送不同的内容
app.get('/', function (req, res) {
if(req.accepts('text/html')){
res.sendfile(__dirname + '/index.html');
return;
}
else if(req.accepts('application/json')){
res.json({'key':'value'});
return;
}
});
这里如果您的请求标头接受'text/html',它将返回 index.html 文件。如果请求头接受'application/json',它将返回JSON响应。
【讨论】: