Score:0

Node.js backend with simple frontend on same server

mq flag

i have /var/www/html/ with index.html

i have /var/www/ai_backend with server.js

server.js is running on port 5000. so if i curl from this machine http://localhost:5000 i get response.

but if i send post request from index.html to server_ip/api i'm getting error

POST http://server_ip/api 404 (Not Found)

this is my nginx conf file

server {
        listen 80 default_server;
        listen [::]:80 default_server;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name <server_ip>; # i have real ip in config file

    location / {
            # serve static frontend first
            try_files $uri $uri/ /index.html;
    }

    # location ~*^/(api|posts|products) {
    location /api {
            proxy_pass http://localhost:5000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }

}

Score:0
br flag

Your configuration looks mostly correct, but there are a few things you can check:

  • Make sure that your backend service is running and listening on localhost:5000.
  • Check that you have reloaded nginx after making changes to the configuration file. You can do this by running sudo systemctl reload nginx on your server.
  • Check the nginx error logs to see if there are any relevant error messages that could explain why the proxying is not working as expected. You can find the logs in /var/log/nginx/error.log.
  • Make sure that your frontend is sending the POST request to the correct URL. Based on your configuration, it should be sending the request to http://<server_ip>/api. If you're using a relative URL like /api, it may not work correctly.
  • Check the access logs in /var/log/nginx/access.log to see if the request is reaching nginx and if it's being routed to the backend service correctly. You should see a line in the log with the HTTP status code and the URL that was requested.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.