-1

im using NodeJS/Expressjs and I have this route:

router.post({

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

});

and /test/test.js looks like

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

Which is the good way to do this? Can I use some templating system?

Regards,

mdv
  • 925
  • 3
  • 14
  • 28

1 Answers1

0

Have a look into the EJS templating system. Does what you are asking.

// 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');
David
  • 2,094
  • 3
  • 30
  • 47
  • Yes I'm using ejs but I want to save the rendered code to a javascript disk file. – mdv Jun 09 '17 at 16:12