res.sendFile发送静态文件+对象 [英] res.sendFile send static file + object
问题描述
我必须用快递服务一个html文件,而且还要发送一个对象和响应。
我如何发送两个 - detail.html和对象'car' - 如何在客户端访问?
I have to serve a html file with express, but also want to send an object along with the response. How can i send both - the detail.html and the object 'car' - and how can i access it at the client side?
app.get('/unit/:id', function (req, res) {
console.log(req.params.id)
var car = {type:"Fiat", model:"500", color:"white"};
res.sendFile(__dirname + '/detail.html', car);
});
推荐答案
res.sendFile
必须设置一些特殊标题(
Content-Disposition
与 Content-Type
配对),所以浏览器将会理解,它的附件,它的文件类型和浏览器的文件,可以显示保存对话框或打开文件
res.sendFile
has to set some special headers (Content-Disposition
paired with a Content-Type
) so the browser will understand that an attachment its comming and based on the file type and browser either show the save dialog or open the file
你可以做的是将车对象发送一个json与 res.json
,并在前端检查json是否被抓取,所以你可以打一个第二个端点将触发下载
What you could do is send the car object as a json with res.json
and in the frontend check that the json was fetched so you can hit a second endpoint which will trigger the download
这篇关于res.sendFile发送静态文件+对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!