简体   繁体   中英

Node.js without a template engine

I'm new into Node.js and trying to learn. From what I have understood it's common to use a template engine (eg. Jade), even for CSS (eg. Stylus). To be honest, all the tutorials I have seen out there involve a template engine when it comes to the layout.

The thing is that I don't want to use a template engine because I think it's unnecessarily complex.

Here is a link to a boilerplate for Node (unfortunately it doesn't work by some reason), but Option 1 shouldn't be using any template engine for the layout.

So, what is the easiest way to combine Node.js and Mongodb with "normal" HTML(5)? Is it possible for example to use HMTL5 Boilerplate with Node?

If you are using static html, so you wont need templating on the server side. You can easily serve your html files with Express/Connect static middleware , for example:

app.use(express.static(__dirname + '/public'));

then put an index.html to your public folder.

Also I think you can copy and paste the whole h5bp to your public folder and it should work.

Here are my thoughts on this.

If you are serving only static html, it's obvious that you don't need any template engine, since you can just buffer the html in the response, or use the Connect static middleware.

However things get interesting when you have to deal with dynamic content.

This is where template engines are good at, since they provide ways to integrate your data with the html. If you are going to replace the template engine, you need a library that can do HTML and DOM manipulation. I can think of two alternatives:

  • jsdom , and libraries that are build on it (such as fill.js ).
    With jsdom you can use server-side jQuery to build your views, or even YUI .
    However it has some drawbacks:
    • it's slow and cumbersome
    • it's a pain to install on Windows since it depends on native modules
    • i couldn't get it to parse html fragments or incomplete html (maybe someone knows a way around this)

  • The second alternative would be to use some lightweight libraries that handle html, without the full DOM. So far I've found two libs that are good at this:
    • cheerio - a small library that relies on jQuery -like selectors
    • plates - a library that binds data to markup

Both are very neat in my opinion, and a good starting point in getting rid of templates:)
There may be others that I'm not aware of, but you get the idea.

Using express, you would just send the html5 in the response:

app.get('/', function(req, res){
  res.send('<header>Hello World</header>');
});

However, I would say that in most cases a templating engine doesn't add complexity. The separation of concerns makes things simpler if you are rendering dynamic content.

First time answering my own question. I just want to share that I found a converter from html to jade (template engine). This is definitely a good thing that's removing a lot of complexity at least for me, even if it still involves a template engine.

http://html2jade.aaron-powell.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM