I'm quite new to node.js, having migrated from php... So in php you can create say a file called headcode.php and you can put tour navbar there and anything else that needs to be at the beginning or in the head of all your pages, then you can go ahead and include it in all pages on your site.
My question is, how do we do this in node.js? I tried fs.readFile but it only showed html with no css
And i noticed i can't type node.js in an html file, neither can i type html in a .js file without doing res.write for each line of html which is rather tedious.
How can i implement this php ideology of "headcode.php" into node.js?
I reccomend using the express framework (expressjs.com). There are a lot of good and free tutorials on how to use it. And with the use of a Templating Engine like EJS, Handlebars or PUG you can use partitials and even a base Layout (Exception EJS, you need another npm module for this (express-ejs-layouts)
Here are the Docs on howto install the Templateengine of your choice using express. (expressjs.com/en/guide/using-template-engines.html)
Related
I recently started learning nodejs and now i am very confused between what to use for my web app, html or ejs (Express). Ejs uses the Express module whereas the .html uses the HTML module. My first question is, what are the differences between them if exact websites can be made from both. The second question, that extends from my first question is what should one use for a website that is to be uploaded to the cloud. .html or .ejs. I believe .ejs is dynamic (if statements can be used inside the file) therefore .ejs should be used. But, on the other hand, its the first time I am hearing this .ejs name, and I am uncertain as to what future it holds, whereas HTML has a build up name, and most developers and the market knows what it is.
Thanks in advance.
Ejs is a template engine.
It makes possible for you to load data from your application in the view.
After the template is rendered, it generates a .html file for the browser.
Answer 1: index.ejs allows you to execute JavaScript inside html element. For example: loop or perform arithmetic operation. index.html can't include JavaScript inside its elements.
Answer 2: of course, index.ejs will come to my mind first. If you learn php, it is the same. php developers use index.php instead of index.html
Answer 3: you don't have to worry, there are plenty of supports from JavaScript community today.
I am a beginner in express and node, coming from a PHP background. What I used to do in PHP, split a file into header.php, footer.php, sidebar.php etc.
And finally, include them in a index.php or other pages. Is there any way I can do it using express. I am using node and express to serve a HTML file.
All I want is to split them into header.html, footer.html etc and add them in index.html or other pages. Currently, I can serve only a single HTML file using express and node. Any help would be appreciated.
You just have to pick a template engine to do something like that. Here is a list that you can pick:
hogan
jade
handlebars.js
This is quite easier than you think.
Follow the link :
http://expressjs.com/en/guide/using-template-engines.html
The templating engine used there is Pug(upgraded version of jade). You may also choose to use any other one too like that Handlebars, EJS etc.
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).
what template engine is recommended for NodeJS when using jQueryMobile Multi-Page template?
I need logic in every view such as include_once('html-header') because I never know is current page is first on rendering list (aka does it match session-privilege criteria).
I've tried Jade but it seems to be completely impropriety solution for such problems. But maybe my design is wrong?
Probably something simmilar to PHP with <? and <?= will be best to fit my project needs.
Edit:
I'm using nodejs as a HTTP server.
You can use emberjs and detect the states and you can append and delete views as you need them to the outlet, Ember uses handlebars template engine to render html
Emberjs
Also check outlets in this page