consume API from node JS - node.js

How would I consume any API from my backend in node JS.
Let's say that I want to make only use of a few routes but that they do not hit the API from the frontend but that the response is made by the backend that previously has already developed that query logic.

Related

How to refresh a React component after a request to backend Node app?

I have an application composed of:
React frontend
Node JS + Express backend
Mongo DB
Backend and frontend communicates mainly through Apollo GraphQL.
Recently I began using Twilio for making calls. After a call, Twilio sends a backend request to my node JS app to a specific route, which is then modyfing a document in Mongo. The request is totally independent of the frontend and can come anytime.
How can I update the React component - a form displaying mentioned document, after that backend request (the displayed data should change accoridng to backend version)? I am thinking about websockets and/or Apollo subscriptions, but maybe there is some easier way?
Twilio developer evangelist here.
You are looking for a way to push data from your server to your front end. Web sockets is probably the best solution for you, and since you are already using Apollo, and Apollo Subscriptions are commonly implemented over web sockets, then that is probably a good solution for you.

Using Node.js API on my react-native frontend

I am new to react-native. I am using an api that basically returns json data, I am able to easily use json on my frontend but I am stuck because I don't know how to redirect to my other frontend file using same api but different route.

Combining Vue.js app and Restful API service

Is it possible to combine a Vue.js app with a restful API service?
New to Vue but written my first Vue/Typescript app that is a front end that consumes some public APIs, add extra logic and display results. All works – fine. Now I also want to have my own restful API interface.
I know Vue apps are SPAs, so when I ‘change page’ to myapp.com/page2, it is not fetched from the sever but rather the app just re-renders the display. But is it possible for those routes that are not defined, such as myapp.com/api/whatever, to be a restful interface? That is, consumes requests and return responses? And when hoisted, would a user have access to both a front-end and a back-end, even though the app is running client side?
Did start down this route using express.js but ran into issues as if I was trying to combine chalk and cheese. (Not lease, would need to change the node module to ‘commonJs’).
Would using Nuxt help? Know nothing about Nuxt … yet. Or must I go the traditional way, and write a separate back-end and change my front end to consume it.

Best way to add a React frontend to a existing Node.js / Express REST API backend

I have an existing backend with a REST API written using node.js/express.
I can call urls e.g. /getallhouses and get the corresponding JSON object.
Using a mobile app, this is rather straightforward as you just call the REST API and process the data.
As I need to also add a webapp e.g. with React.js, it seems more murky.
What is the best way of implementing this webapp strategy?
Creating a stand-alone react.js app which will be called by the user first and then is using the REST API like a mobile app and hosting that react.js app on a different server?
Or is it better to start from the already existing express/node backend and serving the initial index.html file by
res.sendFile(path.join(__dirname + 'views/index.html'));
and putting all react.js related code into views and letting the backend serving the required react.js app to the user?
The best way is probably that you create a react app with redux and compile it to a bundle with something like webpack. Then you could host your page completely static.

Store http request to variable then have seperate http request pass to frontend

I’m planning some changes to how users interact with the data served on the front end of my web app.
This app streams news feeds from twitter. Currently the front end built with angular sends an http request that is passed to a route in my node backend. The backend uses the Twitter api to load the response. As my user base grows though the requests will be too expensive to maintain. Instead I want my backed to use a timer and make a Twitter api request, Store the data in a variable and then serve that data to the user on the front end.
What would be the method for me to do that?
Backend:
1. Node http.request to Twitter API using timer
2. Seperate route that handles the incoming request from the angular front end.
Front end:
3. Http request from front end that sends the request to the route (2) in the backend.
I thought I could use promises but i’m now sure how chaining would work here.
For your backend server I'd suggest to create an API cache with Redis to optimize the performance, due to less information about your code, I can help you with an example to create your own one by implementing it with your Angular and node due:
https://www.compose.com/articles/api-caching-with-redis-and-nodejs/
hope it help you!

Resources