res.sendFile send static file + object

前端 未结 2 488
悲&欢浪女
悲&欢浪女 2021-01-20 09:08

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

2条回答
  •  独厮守ぢ
    2021-01-20 09:43

    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.com/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:

    <%= title %>

提交回复
热议问题