Can I use plain HTML to write views in the Express framework?
So I started to use node.js along with the Express framework, but I do开发者_JAVA百科nt know how to get rid of Jade and all the other templates... I want to write my views in plain HTML. How can I do it?
If you want to use plain HTML, you don't need to use views at all -- just serve the HTML pages as static files. If you want to be able to use your node.js variables, or include files within files, then you'll have to have some kind of templating language.
I know this is an old question, but maybe others have come across this page as I did and are looking for a good solution that lets you write typical raw HTML as your template, and maybe even lets you add variables into your HTML like <b><% = myvariable %></b>
that they can send in from their Node.js javascript. Or they could just use plain raw HTML.
The solution is EJS.
You can do it this way:
Install ejs in the root directory of your Node.js project:
npm install ejs --save
(
--save
also saves ejs to yourpackage.json
file if it exists)Set your template engine in app.js as ejs
// app.js app.engine('html', require('ejs').renderFile); app.set('view engine', 'html');
Now in your route file you can assign template variables
// ./routes/index.js exports.index = function(req, res){ res.render('index', { title: 'ejs' });};
Then you can create your html view in /views directory.
Here's a full step by step walk through of the process of getting EJS and Express working: http://robdodson.me/blog/2012/05/31/how-to-use-ejs-in-express/
See http://www.embeddedjs.com/ for more information, and of course you can Google "ejs express" no quotes.
精彩评论