Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a View Engine necessary for Express/Node?

So i've dove into Node/Express head first (w/Learning Javascript Intermediate stuff) and im a bit confused at why a "view engine" like Jade or EJS is required?

I can't seem to find something that uses just normal HTML5? Or is it that I cannot pass values to normal HTML with express?

Lets pretend I had an index.html page that loads and when I "Login" it loads with the user name posted at the top (Just for example). Can I not pass values to a normal .html file within express?

like image 939
msmith1114 Avatar asked Apr 01 '17 02:04

msmith1114


People also ask

What is the purpose of view engine in Express?

View engines allow us to render web pages using template files. These templates are filled with actual data and served to the client. There are multiple view engines, the most popular of which is Embedded Javascript (EJS).

What is view engine in Node?

View engines are useful for rendering web pages. There are many view engines available in the market like Mustache, Handlebars, EJS, etc but the most popular among them is EJS which simply stands for Embedded JavaScript. It is a simple templating language/engine that lets its user generate HTML with plain javascript.

Is it necessary to learn Express for Node JS?

No, It isn't, ExpressJs is framework build on top of nodejs, as they are many framework in different programming language it is the same for Javascript in the backend side. In the nodejs world ExpressJS is the popular one, so in many books it's normal to talk about It, as Javascript was firstly build for the web.


1 Answers

Is a View Engine necessary for Express/Node?

No, it is not required. Express can happily serve static HTML5 files as you wish. You don't need a view engine for that. You can either create custom routes and use res.sendFile() for each page or you can use express.static() to automatically serve a whole directory of static HTML files or you can write your own code to construct whatever HTML5 content you want to send and use res.send() to send it.

Where a view engine is required is if you want a template-type of system where you can create an HTML template with placeholders for dynamic content and then have dynamic values inserted into the page on the server.

Can I not pass values to a normal .html file within express?

No, you can't do that with regular express. Express has facilities for serving static HTML files, but not for inserting dynamic content into an HTML file. That's what you use a view engine for. Express did not build that capability in itself because there are dozens of different view engine philosophies and Express didn't want to mandate one style so instead it supports a view engine interface for rendering from a template and you can select which view engine you want to use.

like image 193
jfriend00 Avatar answered Oct 27 '22 05:10

jfriend00