Loopback with ExpressJS - javascript

I have examined and practiced on creating Rest Api's with loopback and ExpressJS seperately
While Using Loopback;
It was really time consuming to read all the documentation and learning loopback specific stuff
It also enables you to create your Api in a short period of time and has lots of magic things inside it.
I saw that if I face problems while developing with loopback. I usually get stuck in finding answers from community.
While Using ExpressJs
You write almost every api endpoint in same format with lots of copied code.
You are comfortable with it and can do anything with ease.
But time consuming if compared with loopback.
My point is to utilize best parts of both ExpressJs and Loopback
So my question is "Does it make sense to use Loopback with ExpressJs and also use Mongoose?"
Related with my question Loopback has a documentation about adding ExpressJS route in Loopback application. Loopback with Express Route
If it makes sense is there any recommendation for folder structuring ?

As #Jspdown wrote in his comment to your question Loopback is really based on Express, so you don't need to make choice between them.
When you work on your application using Express and produce 'lots of copied code', soon you start to optimize it and in fact develop your own framework. So this is exactly what Loopback team already have done for you.
Thus the choice is not Loopback or Express but it is - to invest into study ready-made framework or to develop your own framework. As for me I think that if you are working on relatively simple projects and/or quick prototypes or going to change your programming tools soon - don't study, just do. But if you are working on large, mission critical system in long-lasting project - Loopback is helpful.

Related

Service and Controller Layers, Business Logic and Project Structure with Node.js

I come from a decade of experience with Java EE, SE and Java with Spring. If there's something that was drilled in me by myself and other fellow developers, was how to make use of design patterns, separation of responsibility, separating definition from implementation, interface segregation, etc. Also, we were always worried about testable code (unit testing and integration tests).
When I was learning Java, there wasn't a single book, magazine or website that didn't implemented things with those rules in mind, to the point of boredom. So we always new how things should be done from an architectural point of view when starting new stuff or maintaining code. With time, projects like Spring Boot and JBoss Seams started to give out of the box a basic project layout witch you simply followed to success(or at least you should...).
Now that I delve deep in Node.js, I miss that so much. There seems to be only a handful of people on the web worried about it and trying to teach newcomers how to write good backend code with Node.js.
People teach you how to boot express and put all of your code (database, logging, mailing, error handling and so on) inside an express route.
Of course, when I started learning Java, there was nothing preventing you from using Servlets directly with some JDBC code thrown inside, or even worse, Scriptlet inside JSPs...
I would appreciate so much to know how you for instance implement a service layer, where do you put your business logic? Do you put it in classes or functions that returns other functions? How to write easily testable code by Mocha for instance... Do I really need a controller layer, or can I trust my routes to cover that responsibility?
I'm interested in JavaScript only on the Backend, using Express.js, Socket.io, RabbitMQ, Passport.js, and GraphQL(in the future). Not interested in any rendering engine or template engines, much less JavaScript on the Browser. I'm already very proficient with Sequelizer and Mongoose.
I know the feeling of switching from Java Spring Boot to the Node.js stack. It was confusing for me in the beginning aswell, but with time you find your set of frameworks you like.
I'm probably not qualified enough to answer your entire question, but I can give you some advice of what framework helped me the most after starting with Node.js.
For me, this framework is NestJS.
It's a progressive Node.js framework to build scalable server-sided applications, based on Express.js. It supports a lot of different design patterns, but I personally use a pattern consisting of controllers, services, repositories and providers. You can use multiple different databases obviously.
Nest.js stands out for me, because you don't need any 3rd-party tutorial or documentation, because their own documentation is done really well and has basically all the information you'll ever need. It is really expandable with additional dependencies (most of them are in-house developed and have great documentation aswell).
What also has to be mentioned, is that as a Java-dev you will probably miss OOP if you switch to JavaScript. In Nest.js tough you only develop with TypeScript and overall it gives you a feeling very similar to Java Spring Boot.

Web UI for a REST API

I wrote a REST API in Node.js (with Express)that implements a queue with some dequeue/enqueue functionality, and the next step is creating a web view for that API.
I am really new to javascript and Node.js , can i have some pointers on how to implement it? is there a framework I can use to integrate the api.js i built with a front-end?
When you wrote a REST api using nodejs and the expressjs framework, you are perfectly prepared to use the complete MEAN stack, which implements angularJS for frontend services.
Angular offers a complete REST implementation (called ngResource). Install via npm.
With the angular resource service you get your frontend interface (to interact with your backend) out of the box (except a few adaptations you'll have to make, e.g. to make it work with Mongoose, which is used to make the access to your MongoDB more comfortable).
You'll find a few superb tutorials, as this one and others on scotch.io are some of them. Google for 'api rest angular resource mean ...' in different selections ;-)
Other links that helped me a lot:
Tutorial from Angular itself
Thinkster.io - complete MEAN stack overview with examples
a helpful tutorial with good examples from Frederik Dietz
Buecheler comes out with this great tutorial, but its more about the backend side - as you might check your code againt the basic standards of a REST backend implementation.
Those are some of the links I noted and that could find ad hoc.
EDIT:
As your headline suggests you are searching for an UI, I suppose IBM Strongloop might be a good hint for you. IBM is strongly interconnected with the development of ExpressJS and offers this API and UI for probably all data concerning questions of your Webapp.

Node.js program structure in MEAN environment

I'm currently working on a web application based on the popular MEAN stack (MongoDB, Express, AngularJS and Node.js). As I am still struggling with some aspects of node.js, I would like to ask for your advice and opinion on a major architecure decision of mine.
As I found Jade rather ugly (notation-wise) and did not want the server to render webpages (for cpu power saving reasons), I chose to always send back prepared html files/templates.
Example:
app.get('/', function(req, res) {
res.sendfile("public/index.html");
});
On the client side, I then initiate further Angular-JSON based communication to realize REST webservices to instantly get and fill in the needed data. Any reasonable arguments against this approach or maybe good points why I should be using a render engine on the server side?
Besides, does anyone know a good online tutorial for a multipage web application featuring user registration/validation, authentication process, rest webservices and so on? Something newbies can take a look at to get a feeling of how to structure a medium to large sized node.js application well? I've been googling for days now but feel that all tutorials I found were always missing essential ingredients or were rather "elementary".
Thanks in advance.
Igor
I started with Node.js like 2 month ago and now I am starting a project following this same approach.
In the same way, I am not using a "render engine", mainly because I am planning to use my client code (angular) to create a hybrid application using cordova (phoneGap).
I learned a lot from the tutorials of Node.js in Pluralshigh:
http://pluralsight.com/training/Courses/TableOfContents/building-angularjs-nodejs-apps-mean
http://pluralsight.com/training/Courses/TableOfContents/nodejs-dotnet-developers
Finally, regarding security, "Passport" is a great library to implement authentication.
btw: I am using Token-Based Authentication.
And the answer to "how integrate Passport with tokens", I found it here:
passport-local with node-jwt-simple

Integration of Node.js and Backbone.js

I have done considerable reading on both Node.js and Backbone.js; I've read some tutorials and done the relevant courses on Code School. I feel that I've got a pretty good idea of the functions that each technology serves in the context of a web application.
My problem is that I don't really know how to integrate the two technologies and use them in tandem. I would really appreciate if someone could point me to a resource which goes through the entire development of an application using Node, MongoDB, and Backbone together.
Many thanks
This is a good tutorial that shows how to setup that entire stack.
http://backbonetutorials.com/nodejs-restify-mongodb-mongoose/
In short...
Node.js
You can use a library like restify to provide a restful API for your client-side Backbone application. It can also serve your static assets for your Backbone application. The example uses restify, but could be accomplished with other libraries like express.
Mongoose
Mongoose is a javascript abstraction layer for MongoDB. This provides an easy way to interact with MongoDB from Node.js.
Backbone
Your Backbone application can utilize your restify node.js backend to handle the model synchronization. You should have plenty of control to setup the routes via restify in a way that makes Backbone happy.
This ebook could be useful (it's on Backbone, but uses Node for the backend):
http://addyosmani.github.io/backbone-fundamentals/
You also may want to look at this book:
http://www.amazon.com/Building-Node-Applications-MongoDB-Backbone/dp/1449337392
There is also Node Cellar Source.
There is not much explanation about the code, but the app is simple enough to get started and understand the basics layouts of node / backbone
It is, i think, just between an 'Hello World' code and a full app.
I use for ap in backbone yeoman https://github.com/yeoman/generator-backbone and you use it with node.js too
create rest api: http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-express-and-mongodb/
code backbone on the client: http://coenraets.org/blog/2012/10/nodecellar-sample-application-with-backbone-js-twitter-bootstrap-node-js-express-and-mongodb/
the backbonetutorials.com restify one is out of date. It's best to use express, especially if you plan to do any authorization. It's also more widely used
This is a comprehensive tutorial on rolling your own blog with Nodejs, Mongodb and expressjs
http://howtonode.org/express-mongodb
It's old but with a little effort you can get it to work and learn at the same time
I have found this to be the most up to date material on using Backbone.js with node
http://amzn.to/1DygKlJ

Can Backbone and Express routers work together in an Express application?

I have built several Backbone apps and appreciate the client-side code structure and organization. I'm moving into Node development, using Express, and I'm uncertain as to how Express and Backbone can work together in the handling of routes.
You need to understand that Node and Backbone are independent from each other.
Node is for server-side (e.g working with a database, api serving etc. ) .
Backbone is a client-side Javascript MVC framework which gives you a structure for organizing your client-side Javascript application. (the application in the browser)
You can have a Backbone application in your client-side and it can hook up to any back-end either Node, Rails, PHP etc...
For more info check MVVM pattern and Javascript frameworks on the client-side.
http://backbonetutorials.com/why-would-you-use-backbone/
http://addyosmani.com/blog/understanding-mvvm-a-guide-for-javascript-developers/
A friend gave me the answer:
Backbone uses hash routes. For instance http://yoursite.com/#foo
Express will use the traditional http://yoursite.com/foo
You can use the routers independent of one another based one which address you path to - a hash route for client-side functions and the traditional route for server side functionality.
Both routers can coexist.
Your question on how Backbone and Express can work together cannot really be answered precisely, because there are really countless ways on which they can work together. Hopefully some of the info below can help you do what you want to do.
First of all, you can use www.example.com/foo (no #) routes on the client side (Backbone) - see the pushState option in Backbone.history.start() documentation. It is possible to integrate the routes on the client side and on the server side. It is not easy to find exactly how to do, though.
Some of the info under those links may help you:
https://github.com/developmentseed/bones
https://github.com/SC5/backbone-serverside
https://github.com/tysoncadenhead/backbone-on-express
http://nerds.airbnb.com/weve-launched-our-first-nodejs-app-to-product
http://blog.andyet.com/2011/feb/15/re-using-backbonejs-models-on-the-server-with-node/
You wrote that you have experience with Backbone but you're moving to Node recently so I assume that you are open to other frameworks than only Express. You may consider using eg. restify (in addition to Express) to make a RESTful service which you can integrate with Backbone.
There are also entire frameworks like Derby or Meteor that cover both client side and server side using one code base and you can share much more than just the routers.
(Also, I've just found this year's (2013) HTML5DevConf talk: Surviving Robots and Old Browsers by Server-side Backbone. I haven't watched it yet but it seems very relevant to your problem.)

Categories