Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node, Angular, Express and the view engine

I recently started using Angular, Node and Express. I have made some working browser-based applications in Angular and am attempting to port them to server applications in Node and Express. (In particular, one will pull data from Twitter and another from Wikidot.)

Since I am using Angular for data injection and templating, I do not want to use another view engine such as EJS or Jade. I want to use the HTML/Angular code that I have already written with as little modification as possible. I'm very confused as to why Express requires me to have a view engine.

What is the simplest way to get this code working as a Node/Express application?

like image 552
Jay Bienvenu Avatar asked Sep 11 '14 17:09

Jay Bienvenu


People also ask

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.

What is 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).

Do you need view engine with Node?

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.

What is the difference between Express and Angular?

Though there is a fundamental difference between the way these frameworks are used in developing web applications, for example, Express is primarily used for backend purposes, while Angular is known for frontend capabilities.


2 Answers

Since you're using AngularJS, I don't think you care a lot about Express's view engine, that's e.g. better if you're using Express to render HTML. Maybe you'll just use that to load your initial HTML (with links to all the .js, .css etc) into the browser (server-side template rendering), the rest of the work is done with AngularJS talking to your Express's HTTP service

Simple way is for you to return JSON from Express, try this function http://expressjs.com/api#res.json

Use AngularJS to communicate with Express, AngularJS renders the interesting things on the browser (client-side template rendering, and more)

like image 172
bakkal Avatar answered Oct 14 '22 02:10

bakkal


The answer I was looking for is this line of code.

app.engine('html', require('ejs').renderFile);
like image 5
Jay Bienvenu Avatar answered Oct 14 '22 04:10

Jay Bienvenu