Node.js without template engine

I am new to Node.js and am trying to learn. From what I understood, a templating engine (like Jade) is usually used, even for CSS (like Stylus). To be honest, all the tutorials I've seen out there include a templating engine when it comes to layout.

The point is, I don't want to use the templating engine because I find it overly complicated.

There is a template link for Node here (unfortunately it doesn't work for some reason), but Option 1 shouldn't use the templating engine for layout.

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

+3


source to share


4 answers


Using express, you just send html5 in response:

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

      



However, I would say that in most cases the templating engine does not add complexity. Separating concerns makes things easier if you're creating dynamic content.

+3


source


If you're using static html, you don't need server side templating. You can serve your html files easily with Copy Express / Connect Middleware , for example:

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

      



then put it index.html

in a shared folder.

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

+9


source


Here are my thoughts on this.

If you're only using static html, obviously you don't need a templating engine, as you can just load the html in the response, or use Connect's static middleware.

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

Templating mechanisms work well here as they provide ways to integrate your data with html. If you are going to replace the template engine, you need a library that can handle HTML and DOM. I can imagine two alternatives:

  • jsdom and libraries that build on it (like fill.js ).
    With jsdom, you can use server side jQuery to build your views or even YUI .
    However, it has some disadvantages:
    • it is slow and cumbersome
    • This is a pain to install on Windows as it relies on native modules.
    • I couldn't get it to parse html snippets or incomplete html (maybe someone knows about this)

  • A second alternative would be to use some lightweight libraries that handle html, without the full DOM. So far, I have found two libraries that are good at this:
    • cheerio is 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 for getting rid of templates :)
There may be others that I don't know about, but you get the idea.

+3


source


First time answering my question. I just want to share what I found a converter from html to jade (engine engine). It's definitely a good thing and removes a lot of complexity, at least for me, even if it still includes the templating engine.

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

+2


source







All Articles