Because of the company an SEO project, need my assistance. This project has been developed for the majority of others, due to SEO needs, using the server-side template engine.
The back-end colleague of my project said that the project was a go-to-speech write, running the project trouble, only gave me template and css static files.
It took a little time to run a set with Nodejs in order to make it easier for me to debug the template.
Don't talk about it, it's a lot of online.
mkdir appnamecd appName/----savenpm install mustache-express--Save // There are some online with stache, but look at its registered template engine is app.register () should be the former, I tried to find this later mustache-express
Just for the convenience of debugging, the directory structure is more casual
No nonsense, directly on the code, App.js:
varExpress = require (' Express ');varRF = require ("FS");varMustacheexpress = require (' mustache-express '));varApp =Express (); App.use ('/static ', express.static (' static '));//static File HostingApp.engine ("Mustache", mustacheexpress ());//NPM installed mustache does not provide a template engine, do not register the template engine will error Error:module "mustache" does not provide a view engine.App.set (' views ', './templates/zh-hans/')); App.set (' View engine ', ' mustache ');//app.register (". Mustache", require (' stache '));//The first time to find the above code, but the API replaced the error. View API documentation is now App.engineApp.get ('/',function(req, res) {Res.send (' Hello world! ');}); App.get ('/zh-hans ',function(req, res) {Rf.readfile ("./mock/index.js", ' Utf-8 ',function(Err, data) {//read the analog data you write if(Err) {Console.log ("Error"); } Else{Data=json.parse (data); Data. Languagedisplay= "Zh-hans"; Res.render (' Index ', data);//fill the read data into the template } });});varServer = App.listen (3000,function() { varHost =server.address (). Address; varPort =server.address (). Port; Console.log (' Example app listening at http://%s:%s ', host, Port);});
And then
Node App.js
Now http://localhost:3000/zh-hans/can access it.
Isn't it simple?
The code for using the template engine is 5 sentences
var mustacheexpress = require (' mustache-express '); App.engine ("Mustache", mustacheexpress ()); App.set(' views ', '/templates/zh-hans/'); App.set (' view engine ', ' mustache '); Res.render (' Index ', data);
Using the Mustache template engine in nodejs+express