3

I am new to expressjs and started building a project with MERN Stack.

I am confused as which view engine should I use jade\pug, EJS, React.

I have already used jade to render pages with content from mongodb into the browser. Should I use the backend to serve the content in json instead of rendering the whole page as HTML.

I also tried to implement https://github.com/reactjs/express-react-views so that I can use React to render page from server. But there are some problems with 'express-messages' and 'connect-flash' npm packages.

gaurav rathor
  • 187
  • 1
  • 2
  • 9

1 Answers1

6

In my humble opinion, it depends on your preference. Actually it doesn't matter which template/view engine you pick, as you're just learning and practicing. Their learning curve is steep.
You can even learn them for free by watching some quality youtube vids, and you'll get the whole picture.
They're just tools, nothing more.

Personal opinion: To name two popular ones that I personally use: Pug (jade) and EJS.
EJS is without any fuss, keeping the appearance of a normal html file.


There, you had your answer. BUT:

However, in reality, if you work for someone else or might be your own personal preference later, you will be dealing with API designs like: REST and GraphQL.

It means your view is separated from server-side, which is a part of MVC architecture, leaving it to the client-side to deal with it, and that's where RESTful/GraphQl API provides the communication online between server and client.


So that means you have to pick a frontend framework, of popular ones, React, Angular, or Vue.
Picking a frontend framework is far better than leaving your view to some template engine. Not only more flexibility, but also makes you more desirable in market.

Since you're dealing with React already, as a MERN stack, and your personal preference is to have it all server side, Next.js would be a great option to try.

Even at the website meant for MERN Stack fellows (https://mern.io/), to quote:
We recommend using Next.js for your React projects.

Next.js is basically React on server side, that allows you to build your View with a strong framework, instead of a template engine, with so much flexibility and power over it.


Since you're using express as a framework for Node.js, this article is useful for you to use Next.js with express:

Building a server-rendered React app with Next.js & Express.js


Some articles that will be interesting to you:

Server Side Templates vs REST API and Javascript Front-End

Is there any reason to use server side rendering instead of HTTP API + JS Frontend? [closed]

Matin Sasan
  • 1,835
  • 1
  • 13
  • 26