For example I see Node.js a lot of peopple have been mentioning using jade or swig. I am building a Node.js app that will use RESTful interface for communicating with the client. I want to be able to send and receive updates. A lot of tutorials I've been seeing or apps I have been seeing online use jade or some other template engine.
What is the advantage of using a templating engine on the server vs. just serving up a bunch of regular html/javascript and allowing the client to render certain pages based on what information is sent to it from the server? How does a server-side tmeplating engine improve a use of a RESTful interface? edit: is there any advantage or is it just personal preference?
If it is advantageous in some way, I will invest the time to learn it now before I get too far in my project rather than wish I had learned it earlier :-D
Here is an example that uses Jade-lang.
SGML-like markup languages are hard for both computers and programmers to read. Template engines usually help speed up programming by using smaller tokens and syntax. Also, if you go through a pre-processor that templating requires, you can add variables and logic usually.
The downside is that it takes cycles to render the template. Usually though this isn't a problem, as most projects are hindered by time-to-market and not lack of processing efficiency.
I use Jade for most of my projects, because it's very easy to transition from HTML to Jade and also intuitive. I had hardly any learning curve difficulties going from HTML to Jade.
An advantage to templating engines is as they are running server side you can do permission checks directly within the template. You could even send all of the user's data inside of the template all in one request.
They have their upsides and downsides, but security is a big upside as you're not relying on the client to ignore the data its not supposed to see.
There are other ways to do this but this is for sure one of the easiest as it's all done in a single request.
I also want to note real quick that they are not replacements for other forms of permission checks, you need those too. They just help in not putting too much trust into the client.
Related
I am learning Node JS. It had almost learnt 70%. I am very interested in Backend Development but I'm not interested in Frontend development. My question is how do I practice my Node Skills. I only know HTML, CSS in Frontend. How can I make projects in Node JS without knowing Frontend? Or do I need to learn frontend frameworks such as React too to make projects.
You can build an API and use postman to make requests. Start with something simple and keep improving by refactoring your code.
You can work on the API creation part but in my opinion, you must learn one frontend framework it will not only increase your skill sets but also help you to coordinate with frontend easily and take more advantage of it than the projects you will build will be more useful and advanced as well.
You could also focus on implementing some algorithms and data structures starting of with simple ones such as LinkedLists, ArrayLists, sorting algorithms and then move on to (binary) trees and graphs.
These will be required for any programming language to solve (complex) problems and there are many books and internet resources for this. Just search for algorithms and data structures and you'll get plenty. You don't need more than a console application with some simple console.log() statements to implement algorithms - no need for fancy UI.
Last but not least, you should certainly have a look at TypeScript if you want to develop more complex backend applications.
If you really want to do Web Development though it is certainly helpful to have a basic understanding of frontend development or know the basics of common frameworks such as React, Vue, Angular or Svelte.
As others have mentioned for API development you could also just use Postman to send requests.
I have built a Rest-API with Express and mongoose, now it's time to connect it to a Frontend and I am bit confused, I have started building the Frontend of the app with hbs as the templating engine and that works well but I was also considering React for example and that brings me to my question.
What is the best solution here? to build the whole App in one folder so to speak, with a templating engine taking care of the Frontend or to create the API, host it and then use it with a Frontend application? It is a matter of preference or one is better than the other?
This really is a matter of preference. There are many benefits and trade-offs for each method. Here are some:
API with Single-Page Application
Benefits
Easier to make a more dynamic app
With an API you can allow third-parties to integrate with your app easily
Fast - after the app has been initially downloaded only data needs to be transferred!
API and frontend separation can help keep business logic in one place (on the backend)
Offline and caching are easy!
Downsides
SEO isn't as easy (but still very much possible)
Slow - if your app is big, the initial download speed can be slow (there are lots of solutions for this)
Multi-Page Application
Benefits
Fast (page download can be faster)
SEO is slightly easier
More secure by default (due to cross-site scripting on SPAs)
Downsides
Slow - unlike a SPA, you have to download every page
Harder to build and debug
This is by no means a comprehensive list of trade-offs but hopefully, it will help you make an informed decision. Personally, I prefer the SPA approach because I have multiple sites/apps using one backend as well as the ease of development.
i want to create a website based on node js and mysql , but i've read that there is a framework called express for node js , and i'm wondering if i must to use such kind of a framework to create a decent website or it is possible without it and just work with pure node js.
No framework is required. You can write a full-blown web server using only the http module or if you really want to write everything yourself, you can even do it with only the net module.
It's really about what is the most effective use of your time and skill as a developer. Except for academic or pure learning experience reasons, if you're just trying to accomplish a task as efficiently as possible and free, pre-existing, pre-tested code exists that makes your job easier, then that's a better way to go.
For example, if I need to do a file upload from a browser to my back-end and the data is coming in as the multipart/formdata content-type from the browser, I have zero interest in reading and learning the multipart/formdata RFC then writing my own code to parse the multipart/formdata content-type. Pre-existing, already tested code exists to do that for me and I'm adding no value to the goals of my project by re-implementing and then testing it all myself. Therefore, I'd like to use a pre-built module that does all that for me. I can just configure the right library on the right route and out plops my uploaded file in only the amount of time it takes to understand the interface to the 3rd party module and how to use it properly.
This is where Express comes in. Not only does it offer a useful set of features and architecture for configuring routes, installing middleware, sending responses, using template engines, handling errors, etc... but there are also thousands of third party modules that are built to hook into Express and it is easiest to use them if you're using Express as your core framework. Some of these modules could be used outside of Express, some cannot - it really depends upon how they're designed and what Express interfaces they do or don't use.
Also, Express is fairly "un-opinionated" and fairly "lightweight" which means it doesn't force you into a particular methodology. It just offers you easier ways to do things you were already going to have to write code for yourself.
Look at it this way. When you get node.js, there are thousands of APIs that offer lots of already tested things such as a TCP library, a file I/O library, etc... Those are frameworks (in a sense) too. You don't have to use them either. You could rewrite whatever functionality you need from scratch. But, you wouldn't even think about doing that because tested code already exists that solves your problem. So, you happily build on top of things that are already done.
One of the BIG advantages of coding with node.js is getting access to the tens of thousands of pre-built modules on NPM that already solve problems that many people have. Coding in node.js with a mindset that you will never use any outside modules from NPM is throwing away one of the biggest advantages of coding with node.js.
could you tell me what are the Routes used for in frameworks?
A route is a URL that you wish for your web server to respond to. So, if you want http://myserver.com/categories to be URL that your server responds to, then you create a route for /categories so that you can write code for what should happen when that URL is requested. A framework like Express allows you to create that route very simply with just a single statement such as:
app.get('/categories', function(req, res) {
// put code here to handle that request
});
This is just the tip of the iceberg for what Express supports. It allows you to use wildcards in route definitions, identify parameters in urls, create middleware that does prep work on lots of routes (such as check if the user is logged in), etc...
You don't have to use a framework but it is recommended to use one of them since frameworks like Express make your life easier in many ways. Check this: What is Express.js?
Yes you CAN write a Node.js-based backend without any back end implementation framework such as Express. And if you are using Node.js for the first time without any previous experience of asynchronous coding, I'd advise against using Express, KOA or other Node implementation frameworks for your simple learner apps (e.g. those needing things like register/login form processing, logout button, user preference updates to database, etc) because:
(1) Node.js is a core skill for JavaScript back ends.
Stupid analogies between server tasking and restaurant waiters are no use to a real web engineer. You must first know what exactly Node can/cannot do in the server CPU that makes it different to most other back end technologies. Then you have to see how the Node process actually does this. Using Express/KOA/Hapi/etc you are sometimes effectively removing the mental challenges that come with a Node back end. Any time-saving is achieved at the expense of gaining a proper working understanding of what Node is and how it really operates.
(2) Learning Node.js and its asynchronous coding is hard enough without the added complication of coding with an unknown framework like Express/KOA that assumes users' familiarity with JavaScript constructs like callback functions and Promises. It's always better to learn something in isolation so you get the essence of its individual effects, rather than the overall effects if used with other packages/frameworks. So many of these Node.js Express tutorials are the software equivalent of learning to make a cake by watching Momma do it. We can copy it but we don't know how or why it's working. Professional coders can't just be good copycats.
(3) Available learning tutorials using Express often drag in other technologies like MongoDB, Mongoose, Mustache, Handlebars, etc that make learning Node.js even more awkward still.
(4) A share of basic web apps can be written more efficiently with Node.js, custom JS and existing JS modules imported off the npm repository rather than with Express.
(5) Once asynchronous coding and the JavaScript constructs available to assist with it are understood clearly, pure Node.js apps for basic tasks aren't that hard.
(5) After you do get your head around Node.js and can get basic web app functionalities working using server-side JavaScript constructs, you can then judiciously start to explore Express/Hapi/KOA/etc and see what an implementation framework can do for your workflow when doing larger projects needing numerous functionalities. At this point you know what Express code should be doing and why it is done the way it is.
Node.js has become the back-end technology of choice for most small to medium scale web applications over the last 10 years. It is also the major reason why the JavaScript language has evolved from a mere front-end scripting tool with a limited set of Java-aping constructs to the innovative and comprehensive language that it is today. It is also the most popular language in use today. Investing time in understanding the Node server framework, and the latest JavaScript constructs used in Node, is time well spent. Implementation frameworks such as Express, KOA, Hapi, Sails, etc have great benefit when writing more elaborate back ends on the Node.js platform. But all these implementation frameworks are predicated on the behaviour patterns of Node.js. So unless Node itself is understood first, the full utility of Express/KOA/Sails/etc will never be enjoyed.
Try here for the pure Node.js.
Supposed I have a web application that is built using Backbone.js and Handlebars. As server I am using Node.js. Now I want to do rendering on both ends, i.e. on the server and the client.
When a route is requested for the first time, the server shall do the rendering (mainly due to performance reasons). After that, all following actions shall result in client-side rendering.
This means that I must be able to render any page on the client and on the server, both times in the perfectly same way. Both ends have to support the same kind of routes.
How could I accomplish this task?
At the moment, I have taken a look at AirBnb's rendr project, but this definitely ties me to Backbone.js (I'm not sure if I want to stick with Backbone.js for all times), and seems to be not perfectly finished yet. At least, AirBnb does not recommend it for production use yet.
Any other ideas on how to do this?
As a sub-question I might also ask: What is the preferred way to share JavaScript code between the server and the client? For this, I also know piler, but I could imagine that there may be better solutions available.
Any hints?
Well, I am building an application that does this. If you don't want to use rendr, you will have to code your own versions of some of the things they take care of. AFAIK at the moment your choices are rendr or home-grown. Here's some misc tips.
We use cheerio for server-side DOM operations, so when views render on the server, this.$el is a cheerio element instance. In the browser, it's jQuery.
You don't need event delegation and binding on the server side. Our code technically does this at the moment, but it's pointless and a cleaner solution would avoid it on the server
Once you have server-rendered HTML in the browser, you need a way to wire up a big nested tree of view instances to their corresponding elements in the big nested DOM tree. We have a home-grown solution for this, but Backbone.View.setElement is the core and you'll need to write some code to make this happen
We are re-rendering on the browser at the moment although there's probably a slicker way to take a view instance, give it some options in the constructor including a pre-rendered DOM node, and get things properly wired up without re-rendering. That's an exercise for the reader though. :-)
We also send down the raw data we need as JSON within a <script> tag so we have both server-rendered HTML (for perceived performance) and the raw data available as JSON so we can get our backbone models and views instantiated and operational. Again, you'll have to come up with some code to manage this situation.
We use browserify to bundle and share code between server and browser. All of our JavaScript is coded as CommonJS modules.
We have a basic isBrowser() function in our view parent class so we know when browser-only code should run for event bindings, etc.
Anyway, for what it's worth, after working this way for many months, I don't think backbone works well with this paradigm. Many of the core concepts from backbone are fine, but it doesn't lend itself to mapping view instances to pre-rendered DOM nodes. Bootstrapping models and collections from JSON is easier, but the view layer probably needs a significant fork to operate cleanly in this style.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I'm feeling a bit confused, there are so many frameworks out there for Node.js related 'stuff'. Would someone be able to give me an overview of What are the differences between Backbone.js and Node.js? And which is best? Thanks in advance.
I am quoting it from a couple of sources here:
Firstly, to quote from the stack overflow question here:
Most of the things you listed are related only because they are
written in or otherwise use JavaScript. Comparing them is much like
comparing apples to oranges. It's like asking what the difference is
between a Toyota Camry and a V6 engine. They are related, but do
different things.
Node
Also known as Node.js, Node is the JavaScript environment on which we
run our server-side JavaScript code. It is based on the V8 JavaScript
engine. All of the JavaScript code you write, or install and run
from packages from NPM, GitHub, etc. is executed by the Node runtime
environment.
Backbone
Backbone can be likened to as a Model-View-Controller
framework for JavaScript. I believe it was originally written for the
browser; it helps keep your client-side JavaScript clean by
implementing most common MVC patterns (as well as a couple other
things), allowing you to more easily connect your client-side
JavaScript to your server-side code.
Also, this is from an answer for the same question on Quora. Credit goes to Drew Harry:
They're almost completely unrelated. Traditionally, Backbone.js is a
client library and Node.js is a way to write server-side applications
in Javascript. Backbone aims to be a model + view system for binding
data models with DOM elements that represent that model visually in a
web page. Backbone also provides Collections of Models, as well as a
bunch of utility functions for synchronizing those models with their
server-side representations.
Node.js is just the v8 Javascript run-time environment packaged with a
standard library to do useful server-side things with Javascript.
There are lots of packages designed for Node (check out npm for ways
to easily install those packages, Backbone included) that extend it to
do all sorts of interesting things. It's possible to use Backbone.js
with Node.js, but Backbone isn't particularly designed with use on the
server in mind.
Go and upvote the above answer(s) if you find the material helpful.
Pretty much the only things those two have in common is that they're Javascript based and have a lot of hype surrounding them (not undeserved though).
node.js is a framework for Javascript server applications. It includes the V8 Javascript engine developed for Chrome. It's asynchronous and event-driven, so it's ideal for serving large numbers of small requests.
backbone.js is a framework for client-side web applications, specifically for so-called "single page web applications" where only a single HTML page is sent to the browser at the beginning, and every interaction thereafter is handled by AJAX requests and Javascript logic that transforms the page.
This means that the two can also work effectively together: an app implemented using backbone.js for the frontend could have its AJAX requests handled by a server part using node.js - a rather popular combination since it allows you to have an entire web app using only Javascript.
Backbone.js is a javascript library, similar to jQuery or YUI but addressing different needs.
Node.js is a javascript interpreter, similar to Internet Explorer or Firefox or Safari but addressing different needs.
I don't know much about backbone.js but I believe you can use it with Node.js since it uses regular javascript. You may need a DOM emulation layer for the DOM related stuff though.
Additional answer:
A bit of googling reveals that there are people out there using Backbone on Node.js. The advantage of this is obviously you'll be able to use the same framework and reuse code on both client and server.
See: http://nerds.airbnb.com/weve-launched-our-first-nodejs-app-to-product
More additional answer:
With regards to semantics I see that some people disagree what some terms in computing means. While the terms are loosely used, and while they are somewhat interchangeable, they do have fairly well defined meanings.
In general, an interpreter is an executable, that is, a program that takes as input some data and executes it as a program. V8 is not this. It cannot take javascript by itself and run it. It needs to be compiled into another program, an interpreter in order to run javascript.
V8 does ship with example code to build an interpreter though. That interpreter shipped with V8 is called V8-shell.
An engine is a library that implements an interpreter. This is exactly what V8 is.
The two terms above are somewhat interchangeable because the word "interpreter" can also validly be used in place of "engine" to describe what a library implements. But that usage of the word is similar to the usage of "MVC framework" or "UI toolkit" in that it is used as an adjective. So it is correct to say that an "interpreter" is a kind of library.
But the word was originally used to mean the binary that executes a programming language. When used this way one uses it as a noun as it refers to something on the file system. Used this way is similar to the usage of the "compiler". For example one would call clang a compiler in this sense and one would call llvm, the library used by clang, a compiler in the previous sense.
Lets take a look at something that is not javascript as an example:
tcl is a programming language
tcl is also the library that implements the interpreter for tcl. In other words the engine.
tclsh is the tcl interpreter
Let's take a look at another example:
ruby is a programming language
RubyC is one of the many engines for ruby
ruby is the interpreter that uses RubyC
Nobody uses the word "framework" when referring to the binary executable interpreter for the above two languages. It just sounds silly.
But wait you say, Node.js refers to more than just node.exe. It truly provides a bunch of additional features that can be used as a good foundation to write great programs. In other words a framework.
Well, yes. That being true does not make the usage of the word "interpreter" to refer to node.exe automatically invalid. Just as using the word "earth" to refer to the planet does not make using the word to refer to soil automatically invalid.
Besides, those extra functionality? That's true for tcl and ruby as well. It's also true for C. Those extra functionality like fs and http on Node.js are traditionally called standard library. While the Node.js project calls it a framework that's their choice. Almost nobody else calls their interpreter + standard library a framework. PHP for example is distributed exactly like Node.js with a bunch of very high level standard libraries but nobody would call PHP a framework. It's also a bit silly when people write actual frameworks on top of node - frameworks for a framework. But I'm not going to say they are wrong because they choose to call it that. It's just their way to describe what they've created. More power to them.
What I am saying is that people who say that node.js is not an interpreter is ignoring the usage of the word throughout the history of computing. I don't know. Coming from an asian background it's natural to me to assume that everything belongs to multiple categories. Maybe it's a western idea that things belong strictly to specific categories that I don't quite get.
So here are the facts:
Node.js is not simply a javascript library. You need node.exe to use the standard libraries that node ships with.
Backbone.js is on the other hand a standard javascript library. It is not an executable.
Node.js is the only example where an interpreter + library is called a framework so far. All other examples of framework I know of in programming refer to libraries that implement a design pattern.
Calling something "B" does not automatically make calling it "A" invalid.
One final thing: web browsers also come with a very large high level standard library for javascript. It's called the DOM (there's also a bunch of other stuff like Math and XMLHttpRequest but the DOM is the biggest). Accordingly one should call Internet Explorer and Firefox javascript frameworks but nobody does that.
Node.js :
Javascript for backend side. ( like : php, ruby on rails, python, etc. )
Backbone.js :
Javascript for frontend side ( running on the browser of your client )
backbone.js also uses jquery , more frameworks of javascript for client side are :
1. mootools
2. ExtJS
3. dojo
4. prototype
and many more ...
Both are javascript related but totally different.
Node is a interpreter/platform to execute javascript code in the server such as JDK or Ruby. To put it simple, you need NodeJS installed to interpret Backbone based script on the server.
When it comes to server side MVC., Geddy, RailwayJS, Express etc., considered to be serving the purpose better than Backbone.
Whereas Backbone is a champion MVC framework in the client side.
Node.js is a server-side platform designated for building network applications. It is built on Google's V8 Javascript Engine and uses asynchronous event-driven approach for building applications. Backbone.js is a simply javascript client library that makes it easier to create and maintain client-side code and comply with MVC pattern. Hence, they cannot be compared.
Backbone.js has a lot of alternatives that use slightly different approach to achieve the same goal. Most known are: knockout, ember.js and others. And it also can be plugged into node.js application.