9

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?

msmith1114
  • 2,717
  • 3
  • 33
  • 84

2 Answers2

11

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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I tried using res.send on an index.html file in my views folder directory and it said it could not be found. Do I just include it as ANOTHER static file (along with my css/etc...) – msmith1114 Apr 01 '17 at 02:54
  • @msmith1114 - You'd have to show the code you are using for us to know what you're doing wrong. `res.send()` would be used with an in-memory string. `res.sendFile()` would be used with a file from disk. If the file cannot be found, then you must have the path wrong. You would use `res.render()` with a view engine. – jfriend00 Apr 01 '17 at 02:56
  • I was using res.render() thats the problem doh! Im taking a guess most node devs use a view engine? – msmith1114 Apr 01 '17 at 03:19
  • @msmith1114 - If you're serving an HTML site with dynamic content, then yes your job is a ton easier with a view engine. If you're only serving static content or just serving an API, then maybe no need for a view engine. Or some sites insert dynamic content with Javascript in the front end. – jfriend00 Apr 01 '17 at 03:25
3

To answer the main question - a view engine is not necessary for express/node.

You're right though, you can't pass variables/values to pure html. The view engine is something that looks at the data you pass and generates the html markup for you.

Another approach to using a view engine is to use s front-end library/framework like jquery, react, or angular (or you can use plain javascript). The javascript can retrieve data (with ajax/fetch) and bind it to your html - well I guess it wouldn't be bound if you're using plain javascript or jquery, but you can update the DOM with your data.

abdul ahmad
  • 372
  • 2
  • 15
  • So how would I do something like load values into a webpage (from a database for instance) without a view engine. – msmith1114 Apr 01 '17 at 02:53
  • 1
    you can use ajax to send HTTP requests to your server, which retrieves the data from the database, and send it back as json to your front-end javascript request. here's an example: http://stackoverflow.com/questions/9436534/ajax-tutorial-for-post-and-get – abdul ahmad Apr 01 '17 at 02:55
  • Im guessing most users use a view engine anyways? – msmith1114 Apr 01 '17 at 03:20