javascript - res.sendFile send static file + object - Stack Overflow

I have to serve a html file with express, but also want to send an object along with the response.How

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);
});

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);
});
Share Improve this question asked Feb 15, 2016 at 20:45 PatrickPatrick 6,6051 gold badge15 silver badges7 bronze badges 1
  • Related: stackoverflow./questions/33027089/… – user2314737 Commented Jun 18, 2017 at 21:52
Add a ment  | 

2 Answers 2

Reset to default 3

Not really sure about your current setup but you might want to restructure your express app a little. You need to define a view engine and use

res.render('someview', dataObject);

http://expressjs./en/api.html#res.render

with ejs:

app.set('view engine', 'ejs');  

route:

app.get('/', function(req, res) {  
  res.render('index', { title: 'The index page!' })
});

html:

<div>  
    <%= title %>
</div>  

res.sendFile has to set some special headers (Content-Disposition paired with a Content-Type) so the browser will understand that an attachment its ming and based on the file type and browser either show the save dialog or open the file

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745211654a4616877.html

相关推荐

  • javascript - res.sendFile send static file + object - Stack Overflow

    I have to serve a html file with express, but also want to send an object along with the response.How

    4小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信