0

I have a code like res.sendfile('./home.html',{user:req.user}) and I want to access the user parameter in the HTML file. I don't want to use a template engine. How do I do this?

1
  • You're asking on how to create a template engine, while saying you don't want to use one. Dependencies aren't bad things. Commented Dec 9, 2014 at 4:35

1 Answer 1

3

If you don't want to use a template engine, you manually need to read in the file, then push whatever value you want into a placeholder...

var fs = require('fs');
...
  fs.readFile('./home.html', {encoding:'utf8'}, function(err, file){
    if (err) return res.end('ERROR');
    file = file.replace('%USER%', req.user);
    res.end(file);
  });

What you are describing is precisely what templates are for... now, if you want these variables to be available in JS.. then you may want a client "var cfg = %%CLIENT_CONFIG%%;" somewhere at the top, and you can JSON.stringify your config, and do the same replace.

If you want a lightweight templating engine you can use manually, you probably want to use lodash's template method.

Sign up to request clarification or add additional context in comments.

2 Comments

but what is %USER% here? I'm getting an error like has no method replace
%USER% is placeholder you want to replace

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.