Can't POST/GET/PUT anything after deploying - runs fine on localhost - node.js

I'm very new so sorry about this...
Basically, I am trying to replicate a blog from a youtube tutorial - however there was nothing available on how to deploy the application so I'm trying to replicate it as much as I can.
I successfully deployed both the backend and frontend separately on render, then I changed the axios URL to link with the backend. I then type the backend URL with /api/posts and it retrieves the posts in the DB so I am not sure what else I am missing out on if someone could help me that would be great.
Frontend
Backend
Ignore the added extra frontend folder in the backend folder as that was an accident... If that has an effect on this?
Thank you.
ERROR: everydaybeing.onrender.com/auth/register:1
Failed to load resource: the server responded with a status of 404 ()
Tried to link the frontend to backend but unsuccessful

Related

Vercel and Railway deploy

im trying to deploy a simple MERN app with a Vercel front end and Railway back end, working perfectlly in localhost,
so far the backend part seems ok, it works as expected with MongoDB and Postman.
The problem im facing is that whenever I make a request from Vercel, it makes the Request URL start with the client and then the server URL.
For example the Request Url I need (and working with Postman) would be: /backend-production.up.railway.app/movements/
Instead im getting this kind of request: https://client.vercel.app/backend-production.up.railway.app/movements,
No idea why is Vercel using both client+backend URLS and mixing them up like that.
My front end routes look like this:
const response = await axios(`${URL}/movements/`);
where I import ${URL} as "backend-production.up.railway.app"
Thanks in advance and let me know if I can explain myself better if needed.
I recommend adding https:// to your backend URL so will look like
https://backend-production.up.railway.app

Connection working from React to NodeJS, but request-url is incorrect in Chrome Network

enter image description hereenter image description hereenter image description here
Assalamu ‘Alaykum
my React app is running on localhost:3000
NodeJS server is running on localhost:1000
I'm sending request to server 'localhost:1000' and everything is fine, correct response is coming from server, there is no error in Console, but in Chrome Network request-url is to localhost:3000/.../...
if I sent wrong data to server, it's coming correct error and bad request error in Console
can someone help me,
thanks
It sounds like your React app is making requests to the wrong URL. Based on the information you provided, it appears that your React app is running on localhost:3000 and your Node.js server is running on localhost:1000, but your requests are being sent to localhost:3000.
To fix this issue, you will need to update your React app to send requests to the correct URL, which should be http://localhost:1000/auth/sign/in. This can typically be done by updating the base URL or endpoint for your API requests in your React app.
For example, if you are using the fetch API to make requests in your React app, you can update the base URL for your requests by setting the baseURL property in your fetch options:
fetch('/auth/sign/in', {
baseURL: 'http://localhost:1000',
// Other fetch options
})
Or, if you are using a library such as Axios to make requests in your React app, you can update the base URL for your requests by setting the baseURL property in the Axios configuration:
import axios from 'axios';
axios.defaults.baseURL = 'http://localhost:1000';
After updating the base URL for your API requests, your React app should send requests to the correct URL, http://localhost:1000/auth/sign/in, and you should no longer see requests being sent to localhost:3000 in the Chrome Network tab.
It's worth noting that the exact solution for updating the base URL for your API requests will depend on the specific details of your React app and how you are making requests to your server. If you are having trouble updating the base URL, you may want to consult the documentation for the specific library or API you are using.

How to deploy to Heroku with Nodejs frontend, backend, and Docker

There are similar posts, but my case is pretty specific on NodeJS-frontend-backend-Docker-Heroku, so I have stuck for a whole week now.
Task : I have 2 folders frontend and backend. I have Dockerfile in both of them like this /frontend/Dockerfile and /backend/Dockerfille. I also have docker-compose.yml and Procfile on the root directory.
Attempt 1 : change to Dockerfile.frontend and Dockerfile.backend. I use heroku:container push --recursive. Successfuly deploy it on Heroku, I already have scale=1 too. It doesn't work.
Attempt 2 : change Dockerfile.frontend to Dockerfile.web and Dockerfile.backend. The frontend web is working, but I can't login so I can't say for sure. I can't call api request to backend with POST https://mycoolapp.herokuapp.com/api/users/login. I also used Postman and nothing happened.
My guess : the backend either doesn't work at all, or else I use process.env.PORT || localhost:5000 incorrectly.
I really need help with this. Sorry for my english. Thank you.
I solved it by pushing frontend and backend to different apps.
For each folder, I would have 1 Procfile that has web npm start
And then, I would change my API in my frontend from localhost:5000/api/info to myappname.herokuapp.com/api/info
Explain : I believe 'web' process-type is the only special one that accept external HTTP. So my backend previously did not work, while my frontend (web) works.

Connecting front-end and back-end with react and mongo DB - 404 error

I am trying to connect an app hosted in my localhost:3000 port. The back-end is on the localhost:8080 port (not a remote API but on my own PC. I downloaded a pre-created back-end api and linked the front-end to the back-end with MongoDB using an .env file.
The weird thing is that on MongoDB the connection looks ok, following the tutorial I am using. The backend and the front-end also look alrigh, however, I am unable to login with the form in the screenshot.The error I get when trying to login or create a new user is "xhr.js:178 POST http://localhost:3000/login 404 (Not Found)"
It was a bit hard to put the whole code here, so I am linking you to the front-end repo: https://github.com/manuelalonge/complex-app and the back-end repo: https://github.com/manuelalonge/back-end-api
I can understand the issue is most likely on the back-end but I could not understand where exactly. I tried to get back to the previous lessons in the tutorial but it still doesn't get solved.
Probably it is easier to solve this issue with a screenshare session, so if anybody would contact me I'll be grateful.
Screenshot --> [1]: https://i.stack.imgur.com/jVJzn.png
Your screenshot points to what's wrong. You're posting to localhost:3000, as you know this is the frontend webpack server.
You'll want to create an axios config file and set a base url to hit the correct endpoint.
const axiosInstance = axios.create({
baseURL: 'localhost:8080'
});
export default axiosInstance;
Also, please add some sort of file structure.

angular universal https problems

I have an angular universal app set up. I do POST requests on the server-side using localhost to pre-render my app and this works fine.
An example working url would be http://localhost:8000/api/get-info.
I've now put the app into production on an external url (apache server). I'm also using ssl.
Now when I try to do a POST request on the server-side to pre-render my app, I get back a response with status: 0, url: null (I'm assuming this means the connection was refused).
An example non-working url would be https://mywebsite.com/api/get-info.
What really stumps me is that when the app loads on the client, all HTTPS requests start working. So the problem is I cannot get the express server to send POST requests to my external url.
I've tested a post request on the server-side to a different website (twitter), and that seems to work fine as well. So i'm not entirely sure where I've gone wrong.
I already have CORS set to '*' as well.
Try using
http://localhost:8000/api/get-info
in production as well. Since your Angular app is rendered on the same server as your API is running, using localhost should just work fine. It doesn't matter if you are on an external URL.
I do something similar (its a GET but that shouldn't matter) with my translations:
if ( this.isServer ) {
translateLoader.setUrl( 'http://localhost:4000/assets/localization/' );
} else {
translateLoader.setUrl( 'assets/localization/' );
}
It works locally and in production (both server and client).
I just encountered this problem myself for two days. Please take a look at my comment on https://github.com/angular/universal/issues/856#issuecomment-426254727.
Basically what I did was I did a conditional check in Angular to see if the APP is running in browser or in server (rendered by Angular Universal), and change my API endpoint to actual IP in https or localhost in http accordingly. Also in my Nginx setting, I only redirect incoming request from browser to https by checking if the server_name is localhost.
Hope it helps!

Resources