将变量传递给模板

时间:2017-06-09 04:11:28

标签: javascript node.js express templating

我正在使用NodeJS / Expressjs,我有这条路线:

router.post({

   var value = req.body.value;
   //I NEED TO DO SOMETHING LIKE
   var file = '../test/test.js';
   file.render(value);

});

和/test/test.js看起来像

var myVar = {value1};
var someStruct = {key: {value2}}
function Test(){
  var info = {value3};
}

这样做的好方法是什么? 我可以使用一些模板系统吗?

此致

1 个答案:

答案 0 :(得分:0)

了解EJS模板系统。你在问什么。

// server.js
// load the things we need
var express = require('express');
var app = express();

// set the view engine to ejs
app.set('view engine', 'ejs');

// pass and object/string into the render function as the second arg.
app.post('/your-url', function(req, res) {
    let data = {
        object: 'string'
    };
    res.render('pages/index', data);
});

app.listen(8080);
console.log('8080 is the magic port');