Can anyone please explain what is Ejs , can we build a full fledge frontend using Ejs while using node.?
I have been searching for it but i can not find the answer i want.?And please someone differentiate between the frontend frameworks like (angular and react) and Ejs..
EJS is a template system. You define HTML pages in the EJS syntax and you specify where various data will go in the page. Then, your app combines data with the template and "renders" a complete HTML page where EJS takes your data and inserts it into the web page according to how you've defined the template. For example, you could have a table of dynamic data from a database and you want EJS to generate the table of data according to your display rules. It saves you from the drudgery of writing code to dynamically generate HTML based on data.
EJS is compatible with Express for back-end use as it hooks into the View engine architecture that Express provides and lets you render web pages to the client with res.render() in Express.
FYI, there are dozens of competing template systems for use in node.js. EJS is a popular one and people typically choose one based on features that match your needs, how their layout language fits what you want to use, what seems easiest to you to use, etc... I've used Pug, Handlebars, Nunjucks and EJS. Nunjucks is my current favorite.
EJS (along with all the other competing template engines) allows you to generate full-blown HTML pages which certainly enables a "proper front-end".
EJS is a tool for generating web pages that can include dynamic data and can share templated pieces with other web pages (such as common headers/footers). It is not a front-end framework. While EJS can be used by client-side Javascript to generate HTML on the client-side, it is more typically used by your back-end to generate web pages in response to some URL request. EJS is not a client-side framework like Angular or React and does not dictate what client-side framework you do or don't use (if any). It is mostly covers a separate solution space.
Related
I am new in Node.JS stack. I saw a big number of developers are using pug template engine (Express JS) instead of regular HTML.
If anyone of you have used pug can you please tell me the
major benefits of using pug over HTML view technology?
First, let's discuss the Pug framework. Pug framework is a highly flexible framework which supports the generation of HTML content dynamically for almost any sort of data. You can write the Pug template to create -
User Profiles, List of store items, Templated HTML documents, Conditional blocks in the web pages
We can render and create the conditional blocks and iterative structures to produce the dynamic HTML web content instead of hardcoding everything.
Node.js runtime supports Pug templates by default ㅡ the template engine is Jade by default, however, Jade has been renamed to Pug. You can continue using Jade engine, but it is recommended to use Pug templates. Pug templated files are written as below.
doctype html
html
head
title My Page
body
h1 Heading
p My paragraph here.
This will get translated to the following HTML content on demand.
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Heading</h1>
<p>My paragraph here.</p>
</body>
</html>
Fast answer: HTML.
Why? Because it is processed only by the browser, so you are removing one layer of your stack (compiling PUG). It will make your application somewhat lighter.
But PUG is an awesome tool. It offers nice maintainability for your code and a strict code style. It is useful when working with more people and it feels great.
Also, PUG increases my code efficiency by 20% in SublimeText.
HTML is client side rendering. PUG is server side rendering. When you use SPA with HTML the whole shit has to be loaded with PUG only the page has to be rendered. So it depends on what you want. With SPA after the side is loaded only data is transfered from server, no HTML pages. Big websites still use server site rendering. Geeks like Angular etc. to do client site rendering.
It is not accurate to say that one language is "better" than the other in general. Both PUG and HTML can be used to render data to the view page in a Node.JS (Express) application, and the choice of which one to use will depend on your specific needs and preferences.
PUG is a template engine that is designed to be easy to read and write, with a focus on simplicity and expressiveness, while HTML is the standard markup language for creating web pages. Both languages have their own unique features and syntax, and either one can be a good choice depending on the requirements of your project.
For full article refer to https://www.ubercompute.com/blog/benefits-of-using-pug-template-over-traditional-html/
I have recently started learning React and am trying to make a simple blogging app. I store the data (post content, comments etc.) in MongoDB and would like to render the content according to the route, such as, when I have a certain URL (like /blog/:username), I'd pull data from the database and then render a view with the data content.
Using Express, I am now using static html files ( express.static) as the view, yet this makes it impossible to render the content according to the requested route, such as:
/blog/:username/:article
It is possible with a template engine, like Jade, but Jade, as far as I know, does not work with React.
What is the correct way to make dynamic views using React while preserving the URL route structure?
Thank you
Generally speaking, there are several ways to achieve your goal. I'll sketch the one that I feel is the most natural approach when using React:
Create your blogging app with React
Use a frontend routing mechanism such as react-router to make React aware of the URL
Either fetch the data for each blog entry from the backend via an Ajax call each time the user hits a URL, or store the blog entry data in the frontend (e.g. using something like redux) and reuse it when required.
Does this make sense? If not, please keep asking...
I am working on a POC on Node JS, and I learnt that a typical tech stack will look like - Jade (instead of HTML)/ NodeJS/ and some database. My question instead of Jade can we use HTML 5? This is to avoid learning one more language to complete the POC. Also I assume that I will be able to expose the Node JS methods as rest API instead of having PHP or Java layer.
More over if I use simple HTMl/JQuery - for UI and Node.js ( for restful service) it will be easy for one to migrate to other framework easily. Please share your experience.
This is more an opinionated question, so i would like to share my opinion.
My question instead of Jade can we use HTML 5?
Jade is not alternative of HTML5. Jade is a templating engine whereas HTML5 is not. So, both are different.
Getting back to your question, you can use HTML5 as well.
Role of Jade
Ex: Consider yourself in a scenario where after user login you need to display a profile page and in profile page You need to print 'Hello '.
Since is dynamic value, so it can't be hardcoded in HTML file. Therefore, you place a placeholder in HTML (since you have added placeholder and made your HTML file generic for all user, thats why such file is called template file instead of plain HTML file). Now you can fill the placeholder with dynamic value either on server side or on browser.
If you select to replace placeholder by their value on server side, you use some templating engines. Ex EJS, JADE etc. Templating engine are responsible for generating HTML from template
If you select to replace placeholder by their value on client side, then you can choose to opt Ajax calls and fill your placeholder using Jquery or Angular.js may be handy if your project is expected to be big enough.
if I use simple HTMl/JQuery - for UI and Node.js ( for restful service) it will be easy for one to migrate to other framework easily.
IMO, using HTML with jquery for UI is better, since it is simple and traditional and you will get more support on community forum. Also, you wont have to learn template, templating engines straightaway.
I am new to node.js and at the moment I am trying to wrap my head around template engines. It seems like all the tutorials I find on using Angular.js with Node.js are always accompanied with Express.js.
Do I need Express.js to use Angular.js with Node.js? If so, why?
What is the relationship between Express and Node/Angular?
What in the world is a template engine?
Why did I not need a template engine for PHP?
You don't need to use Express for Angular. You could use another framework or just the plain node.js core http/https server directly. Express just incorporates abstractions and conventions on top of the built-in node.js http/https server to make it easier to write your backend logic.
See wikipedia for both a good overview of what a template engine is and a list of example template engines.
Template engines aren't necessary to generate output, but they can certainly aid you in that process by making it easier/simpler to compose output, instead of manually concatenating or echoing a bunch of strings for example. PHP kind of has a "template" built-in, in that output buffering is used such that you can just inject <?php ?> blocks anywhere in the output. This is similar to many template engines, including ejs, except ejs can save the template as a reusable function instead of throwing it all away once the request is done).
Is it possible to generate Server side includes with express.js?
I'm trying to reuse my header and footer markup on different pages so I can make header/footer changes in one place.
Try to use partials.
You have to choose between some template engines. The default template engine is jade, so for this you have to do the following: create a file views/header.jade and put your stuff in. in your action you can render the partial with partial('header');
See the expressjs docs for more.