What does “res.render” do, and what does the html file look like?

后端 未结 2 450
说谎
说谎 2020-12-02 20:38

What does res.render do, and what does the html file look like?

My end goal is to load arbitrary comma-separated-values fr

相关标签:
2条回答
  • 2020-12-02 20:56

    Renders a view and sends the rendered HTML string to the client.

    res.render('index');
    

    Or

    res.render('index', function(err, html) {
      if(err) {...}
      res.send(html);
    });
    

    DOCS HERE: https://expressjs.com/en/api.html#res.render

    0 讨论(0)
  • 2020-12-02 21:15

    What does res.render do and what does the html file look like?

    res.render() function compiles your template (please don't use ejs), inserts locals there, and creates html output out of those two things.


    Answering Edit 2 part.

    // here you set that all templates are located in `/views` directory
    app.set('views', __dirname + '/views');
    
    // here you set that you're using `ejs` template engine, and the
    // default extension is `ejs`
    app.set('view engine', 'ejs');
    
    // here you render `orders` template
    response.render("orders", {orders: orders_json});
    

    So, the template path is views/ (first part) + orders (second part) + .ejs (third part) === views/orders.ejs


    Anyway, express.js documentation is good for what it does. It is API reference, not a "how to use node.js" book.

    0 讨论(0)
提交回复
热议问题