Using the Mustache template engine in nodejs+express

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.