Loopback and Express wiring - node.js

I am planning to build a new application with express (for frontend) and loopback for managing all APIs, hosted on different servers.
How would you typically architect this, would the app (browser) directly make http requests to loopback for data, or would all requests go through expressjs and user never interacts with loopback?
If its the former, how do you do session management? If its latter, would you need to recreate all routes even in express?
Would appreciate some help.

Disclaimer: I am co-author and a core developer of LoopBack.
LoopBack is using Express under the hood. Every LoopBack application is an Express application too, therefore you can use any Express compatible middleware (like session management) and define Express-based routes in your LoopBack project.
It's entirely possible to write a LoopBack application that's serving both REST/JSON API and front-end files, we have users successfully running this setup in production.
As for session management, I don't know what exactly are you asking about. In general, you handle sessions in LoopBack the same way you handle them in Express.
You may find the following resources helpful:
Defining middleware
Use cookies securely

Related

Building scalable SPAs using websockets, how complex is it?

I am beginner in web technologies. While studying about frontend frameworks, I came to know that we run separate application servers for frontend and backend server(API).
e.g. If I am using vue.js, I'll be running a vue server for frontend and I'll be running another MEAN stack API server separately. Now my application will need to show real-time updates, I'll have to use websocket connection between my browser and frontend server which further will need websocket/webhook connection with my backend server(API). Now I am already aware of the scalability and session management issues with websocket connection. In this scenario, how should I build my application for better scalability and with less complexity? Is it possible to create a monolithic application server for frontend and backend? Will that be a good choice?
Is it possible to create a monolithic application server for frontend and backend? Will that be a good choice?
That choice is fine; start simple and you can break into microservices in the future. Unless this is for a large production system!
If you use something like express you can serve the Vue.js files using express.static, API endpoints using express.Router() instances, and the ws or socket.io module attached to express instance for websockets.
Now my application will need to show real-time updates, I'll have to
use websocket connection between my browser and frontend server which
further will need websocket/webhook connection with my backend server
This isn't the case. You can have your frontend (the app running in a browser) connect directly to the backend via websocket if you wish, no need to proxy via a frontend server. For session management look into JWT tokens.
You can go for socket.io library in Nodejs. It's simple and easy to use, The scalability and session can be handled by introducing Redis,
check https://socket.io/docs/using-multiple-nodes/

Node.js - How to share sessions between http server and net server?

I'm trying to make server that helps http clients and net clients to connect between them, but I'm not sure that whether some module such as redis can help the server to share sessions or make connections from http to net.
Because I wanna use cluster (because of load balancing) to optimize speed in multi-core processors, I think the sessions must be shared.
Here is the server what I make
Using Redis is a good approach to save and share session details when using clusters. Read more on this approach here Nodejs Clustering and expressjs sessions.
Another approach would be to use Express Session module if your web application is developed using ExpressJS. This link https://medium.com/#karaxuna/how-to-share-session-across-mutiple-nodejs-http-server-instances-7d466389d123#.wrg84qel6 provides on how to configure Express Session module.

Use express and hapijs together

We have a somewhat big nodejs app using express. We started experimenting with hapijs on smaller services and kind of like it more than express. So we'd like to migrate the express app to hapijs. But since the app is already big, and we don't want to do a complete rewrite at once, but rewrite it step by step, so we can do it in more time. Is there any way to use express and hapijs within the same nodejs process and do the routing between those to by routes?
You should go through on this link:
Hecks
It will show you how to mount your express app onto your hapi server.
You have a couple of options to do it:
You can run those in two separate servers under a HAProxy and decide which server will answer by the route.
You can run 2 separate servers where Hapi will be in charge on all the routes once the route is not found it will proxy the request to express.
Option 1 will have better performance and help you in the future when you need to scale.

What is the best framework and difference of nodejs framework

These days I try to develop real time application using nodejs.
That application want to update the dashboard according to api data.
I installed express and faye and try to compare what is the best and what are the differences of that two.
As I know express is a node base framework and faye is a subscriber/publisher based one.
But I think both are almost same and anyone can help me to identify the differences?
What is fast and what can be done using the frameworks like that ?
Thanks in advance.
They are not very comparable. If you want to create a real-time application, you will probably need to use both.
Express is a web framework. You will need it to serve and handle HTTP requests and responses. It will help you handle things like url routing, request/response handling middleware, interfacing with template engines, etc... Express is as fast as you'll get.
Faye is a pub sub messaging system- It will not be capable of handling standard HTTP requests and responses. You may be able to implement a live stream of the data using Faye, however, you will still have the need to serve up your client side application using Express.
I would also look into Socket.io as an alternative to Faye- in addition to Express.

What's different between connect framework and socket.io?

I'm new to node.js, so i have some questions about connect framework and socket.io:
What's different? i'm confused about it.
Should i use connect fw with socket.io or just use socket.io?
The Connect module is a web application framework, while Socket.IO is a realtime transport module. You would use one to create web applications, and the other for bidirectional communication between a server and a client.
Here's a few of the things the modules can do:
Connect:
service static files and pages
provide cookie-based sessions
accept file uploads
handle HTTP verbs (GET/POST/PUT/DELETE)
Socket.IO:
authorize connecting sockets
send data between server and client with multiple transports
supports (WebSocket/XHR long-polling/flashsocket/JSONP)
So if you wanted to create a website, you would use Connect. However, if you wanted that website to have something such as realtime chat capability, then you would use Socket.IO.
Whether you should use one module or the other, or use them together, is dependent on your application requirements.
Connect is special module which can provide scalable functionality. You can just add features as middleware. It reminds some kind of configuration of your project, it just simplify routine.
var app = connect()
.use(connect.logger('dev'))
.use(connect.static('public'))
.use(connect.bodyParser())
.listen(3000);
After adding this for example you can access features which connect provide. For example you can have logging (method url and seconds) for each application activity, or add session support, easy with one line of code. The same way you can add socket support I suppose.

Resources