How to put MongoDB behind Express and execute arbitrary queries? - node.js

I have an Express application. I would like to expose a route, eg. /mongo and allow users to connect to this route using a client (let's say NodeJS client). The users should be able to execute arbitrary queries, just as they would do normally.
My initial idea was to use some proxy, like express-http-proxy and leverage Mongo's HTTP interface and REST API, but in v3.6 they removed both.
Is that possible?

Related

NodeJS, Express: How to call backend from frontend using ajax?

I am developing a frontend application as well as a backend API (JSON). Both are in Node/Express. In the frontend, I need to call multiple API's in the backend layer. Since both apps run on different port, AJAx calls cannot be made directly. Therefore, I have 2 options:
Frontend AJAX calls a route which initiates an HTTP request to the backend.
I allow CORS in the backend and then AJAX can call directly the backend
In option 1, I found myself creating the same code more and more. Code seems to be redundant and for every AJAX call I have an extra HTTP method in the controller. In option 2, since I am using the package "password", I am not able to pull many of the session content in the backend ince these are 2 different apps (e.g. in the frontend I can simply say "req.user.username", but I am not to do so in the backend). In addition, the user will be able to see in his developer tools the AJAX requests I am issuing to the backend along with some params, which I don't want to happen.
Do you have any comments on the above or can you guide me with some other suggestions?
Thanks

Running api on different servers

I am currently running an express server. It is having routes which is computational and memory extensive. I want to shift that api routes to different server so that it does not harm any other apis.
I am currently proxy passing that request using nginx. But I want to do it in express level in order to get more control.
Is there any different approach or any other npm package to achieve this?
It seems like you are looking for a solution like http-proxy-middleware. Intregation is super-simple too.

How to integrate a Nodejs API with ReactJs app under the same domain

I'm trying to understand how a MERN app fully works, I've been reading about MongoDB, ExpressJs, ReactJs and NodeJs, I also understand how MongoDB, ExpressJs and NodeJs interact and how ReactJs works on its own, my question is simple (I think).
The question:
If I create an API, using Node,Express and Mongo, and I have an APP managed by React, both need a server (via express, I understand), then, how should I run the API and the React app at the same time. Do I need different URLs? should I configure different ports? how should I integrate them?
I really been reading a lot, but almost every tutorial is made locally (and I'm working in a server with Passenger and I can't change the way it starts), just for Node/Express(with pug or else)/Mongo or just React, and I don't understand how to connect the API and React.
Thanks
It depends on several factors: environment (e.g. development, production), and your control over the server. For development, you can have two different URLs and use something like Webpack Dev Server. Normally you would have the module bundler, e.g. Webpack, watching for changes in your React code. However, this can get more complex if you have Server Side Rendering.
For production, normally you would have the bundled file for your client side application already optimized and minified. If you can change your API, you could serve it statically in a new endpoint, for example: /static/bundle.js and request this endpoint from your index.html file, which will be sent by Express.js server when accessing /.
However, because you will probably want to have routes in your React app, your server will need to know how to handle the client app routes (for example app.get('/*', () => ...), and they could collide with your API endpoints. To solve this, you could:
Prefix your API endpoints with a namespace, e.g. /api/v1/...
Place the API in a different URL, port or subdomain. In this case you would indeed need to run these two servers in parallel. With Node.js, there are helpers to make this more convenient, e.g. concurrently.
Pulling out your concerns: API, React, and Integration for MERN app.
I use three approaches
1) Use foreman. With this, you can specify your API and Web Client in the Procfile. I used it here
2) Use a proxy to handle requests that require your API. So in package.json, you specify your API URL(your API must be running)
// package.json
.......
.......
"proxy": "<path to url:[port no if you're developing locally]>"
Check here.
And you can simply add a script to run your API and React concurrently.
3) Set your API and React app in a Docker container. mern-starter is a perfect place to check for this.
Hope this helps!

How can I use multiple servers for different routes in nodejs

I have a nodejs app running on express server. Currently I am using express router for routing my apis. However this backend is being used by multiple apps in the frontend. I am thinking if some apis are being used a lot more than others then we may need to use multiple servers for different api routes based on the load on the server. For example I have two apis for getting players and coaches and I'd like to hit one server when /api/player is hit and the other when /api/coach. I am wondering what is the established way to do this in nodejs?

How to web user interface + node.js and express routing

I have server with node.js + express for routing. When I do requests like http://mydomain.com/photos, http://mydomain.com/photos/123, etc I got JSON answer. This is OK and exactly what I need. So RESTful API is ready. But this is just data and I need also to process this data and show it as webUI (html, css, angularjs).
How to implement webUI for this purpose properly?
I need to have unique independent server API in order to use it for different clients: browser web site, mobile apps, etc.
You probably want something like the MEAN stack: MongoDB + Express + AngularJS + NodeJS.
Your client (AngularJS) is being delivered somehow (static hosting, express, cdn, nginx, etc) and then performs REST requests to an external server (Rails, Express, Php, etc) that has CORS enabled if lives in a different domain. This structure is known as Single Page Applications (SPA), where a single big HTTP request is loaded in order to interact with a backend.
That being said, this is nothing new... things like CouchDB, SammyJS and so on had been working on this for a couple years now.

Resources