Express server, Back-End, API [closed] - node.js

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Hi, I'm new.
started with React.js, by my self, three months ago, on YT.
mostly fetching API
some API's are secured with a signature
didn't know that my API calls must be on the server, not in React
getting CORS error's
didn't know what is and how to fix cors.
started to learning a back-end
every video on YT:
JSON placeholder
SWAPI
no video with React and back-end,
no POST signed request from server video.
almost give up
CORS error solution:
now I know how to fix cors:
with proxy to express in package.json,
adding setupProxy.js in src,
and "app.use(cors())" in Express server.js
My react app:
runs 24/7.
need to update the states every 3s from API.
then compare states-data. (that is some secret formulas and functions that cannot be exposed to the public if that's even possible. To hide code on the express server.js)
and take corresponding actions (new API calls).
Question part:
is that even possible?
how the app is working if no one is on the website?
this means that the Express server.js is always alive
and React UI is alive only when someone open website to see what's going on with data
how to deploy react-app with express? is this good?:
https://dev.to/nburgess/creating-a-react-app-with-react-router-and-an-express-backend-33l3
how to host - where to deploy? On Github pages?
do express server autoruns on deploy or do I need "npm start" on hosting
or open the app for the first time, (open website)
how that works
do I need django
what is better:
if I decide to share my app with other users:
to give them each a copy of react app, and they deploy themselves
or
make a universal app so each user can log in and have their own express server with dashboard and their own data and states.
if the universal app is better were to learn that
Thank you for any question that is answered.

Server runs 24/7
when a user wants to access a page of the website, the server sends it the page's blueprint (the html, css and js, which in this case is built by your react js build code), that's when your react app comes alive.
you already have express for server side, you don't need django.
yea deploying with heroku is fine. it'll take care of the server but you'll have to add a script in the server's package.json to make it build react, if you're not already uploading a react build to heroku.
"heroku-postbuild": "npm install --prefix client && npm run build --prefix client"
here client is the folder you keep your react code in.
If it's not supposed to be public, you shouldn't have it as a react state.
Using a universal thing is better, just make an account system with owner as a privilege, and have the url in the form of www.website.com/owner'susername
and done, every owner gets their own version of the website.
(there's an awesome MERN stack tutorial on youtube by traversy media that answers most of your questions)

Related

React and Express Routing and Views

I'm a newbie in web development. I built a project before using Node, Express, and EJS (as my frontend) and now, I am trying to make another one using React as my frontend.
I watched and studied a lot of React + Express tutorial videos in Youtube but I think every video only shows a one-page site.
My question is how do I handle the routing of Express+React? Where should I put the routes and render multiple pages or views? Should I put it in the app.js under the Node folder? Or should I create another folder under the React app consisting of the js files for different views?
Thank you. I would gladly to answer questions if my post lacks information.
There's an interesting hand to it.
App
__|__
| |
Frontend Backend -->Database(MongoDB)
I think this scanty tree should do justice to it.
I'd suggest you use MERN
MongoDB
ExpressJs
ReactJs
Nodejs
Your apps need to be on different servers. This method is effective and secure.
Since you already know React, Express, Nodejs... You can check out this link (by cleverprogrammer)to see some MERN projects and how each part of the app communicates with one another seamlessly.
https://youtu.be/ktjafK4SgWM

Develop a web site for an API [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm a junior front-end developer, and I want to create a simple API. I have already a mook of this API deployed on Heroku (available on this link), developed with Express.
I want to create a website with Vue, where the root path is, logically, the website, but I also want it to have a path similar to example.com/api/v1/ministries for the API. Basically, I want to create a kind of landing page for the API.
I don't see it as something so complex, but I didn't know how to google it and, therefore, I don't know where to start.
I know how to create applications with Vue and I already have my simple API, but with the knowledge I have it would be applications raised on different servers and I don't want that.
In my mind, I should create a new app with Vue, develop it, install Express in the project and lift the API, but honestly, I don't know how to put those two steps together.
Please make your api in english, the person after you will thank you.
You could make an express app and move the output (dist) of your vue app in the public directory of express.
This way you'll be able to easily add routes
// Move vue output to folder named 'public'
// Point express to this folder
app.use(express.static('public'))
// Send the index.html (vue entrypoint) to the client on request of /
app.get('/', function (req,res) {
res.sendFile(path + "index.html");
});

Making my React app working with a Server

I have a question and got confused with the articles I found on the internet.
I have a react App which deploys on the development server. I want to create an admin part, where the administrator will be able to push content into the database. React App will get content from a Database and render it.
Also after the development, I want to upload this on hosting so users from the internet can have access to it and use it. I understand how the server works with post\get requests from glitch projects.
Writing on React.Js, server with express.js
So can someone explain to me the following:
how the server sends React app? Should I send simple index.html with #root div and the whole website will work?
I am a bit confused about how hand-made server and API are different from hostings? How to connect then backend with a hosting?
IN VSCode is it possible to make a react App showing from the server of Node.js? - On glitch I can
sendfile index.html with simple request and I see the landing page.
So theoretically I should have a possibility to send static file through a server of Node.js/Express.js?
Can you explain these questions to me, please or provide some resources where it explained? Currently I'm watching Youtube and Google but how to connect it with my project I do not get a clear idea.
Pretty much. If you use create-react-app to build your React frontend, the build result folder will contain all the static files required (HTML, CSS, JS, images, fonts...). You can then either have your Express app serve it as static data.
"Hosting" is a nebulous term... No, you can't upload a Express/Node.js app to some PHP web hotel and expect it to work. You'll need an application-hosting centered provider/PaaS such as Heroku.
When developing a react + backend app locally, you'd have your backend server running, and the frontend dev server up to serve your frontend and configured to proxy all API requests to the backend server (like this, via the Create-React-App docs).

Heroku / Node / React - how to access my node api endpoints on heroku

Edit: I can share either of my package.json files if that helps.
Edit2: I dont know if this is a common question or a dumb question. I know it doesn't fit stackoverflow, but I've been struggling with this heroku deploy for the better part of a day already.
I recently followed the following tutorial on posting a react / express app to heroku, and while I think I have been successful, I am confused as to how I can access my API. For reference, a couple of relevant steps in the tutorial are:
Create Node / Express API, push to heroku, and check that it works (by going to the api endpoints in the URL and literally seeing the data), and add "start": "node index.js" to the Node package.json file.
Create React App inside of the root directory of the Node API, add to the react app's package.json this: "proxy": "http://localhost:5000" (I figure so that the api and app can communicate.
I've followed all of these steps, and my React App is working on heroku. The app is displaying, and since the app relies on the Node API to get data to display, clearly the Express/Node API is working.
However, I would like to Actually See the node API endpoints, by going to the endpoint in my browser, and I am not able to do that in heroku currently. All of the React Routes work, however when I visit the api routes my screen is blank. Any thoughts on how i can visit my API endpoints, that are on heroku, in the browser?
For example of my problem:
Localhost Node
when I launch (node index.js) on localhost, I can access my node API endpoints at http://localhost:8080/api/path/to/endpoint
http://localhost:8080/ is root of my node API
Localhost React
when I launch (npm start) on localhost, I can access my react app root at http://localhost:3000/
another one of my react router routes is http://localhost:3000/myreactpage/, which leads to a page of my react app.
Heroku
lets say my heroku app name is https://dusty-manager-23562.herokuapp.com/
https://dusty-manager-23562.herokuapp.com/ leads to the root of my React app
https://dusty-manager-23562.herokuapp.com/myreactpage does lead to my react app page
https://dusty-manager-23562.herokuapp.com/api/path/to/endpoint does not show my data, even though i thought it would / want to see my data. This right here is my question.
So this is my problem. A long post for a simple question - why can't i visit the API endpoint of my node / express app when its pushed to heroku, when I can see the data when its at http://localhost:8080/api/path/to/endpoint. The react app is working, so it's getting the API data correctly, but i want to be able to see the data in browser at the endpoint.
Thanks!
So there are two problems that are probably working in tandem to cause you this headache.
The first and more likely issue is that react-router is intercepting the "/api" urls and deciding prematurely that it doesn't have any components to show for those. A way to fix this is to edit the "proxy" property in your client code's package.json.
"proxy": {
"/api": {
"target": "http://localhost:5000"
}
}
(☝️ I'm assuming here that you kept the 5000 port number from the tutorial. If not change it to 8080 or whatever you used for the api port number.)
The second issue is that the service worker create-react-app adds for you is doing the same thing. It reads the path and thinks there's nothing to serve. If your '/api' route is still showing a blank page, in your browser's dev tools go to the 'Application' tab and click 'Clear site data' (assuming you're using Chrome, not sure how this is done in other browsers) then refresh.
If you don't care about having a service worker, remove the registerServiceWorker() call from your client code's index.js. If you do want the service worker, you'll need to edit the registerServiceWorker.js file to be a little more discerning in how it matches paths.
Hope that helps!
A think this is a relevant post here - Accessing internal API with React, Axios on Heroku
I am not using Axios, but i think the app.get(*) may be giving me issues.
EDIT: no this didn't help a ton actually...

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!

Resources