4

This error occurs when I try to access to the root path ("/") of my MERN application. Though accessing to this path on local machine works fine and I get React application. React app uses 3000 port but server uses 8080. I built my app using this boilerplate: https://github.com/crsandeep/simple-react-full-stack/ (just changing files in "client", "server" and "public" directories and changing paths to client in "webpack.config.js")

I also tried to cover the main component of my app with router (in "index.js" of client) like this:

<Router>
  <Route exact path="/" component={MessageBoard} />
</Router>

But I still get the error. What is the issue?

UPD: Contents of server.js is :

const express = require("express");
const logger = require("morgan");

const API_PORT = process.env.PORT || 8080;
const app = express();
const router = require('./routers/board');

app.use(logger("dev"));

app.use('/api', router);

app.listen(API_PORT, () => {
  console.log(`LISTENING ON PORT ${API_PORT}`)
});

UPD 1: Contents of "/etc/nginx/sites-available/default":

server {
  listen 80;
  server_name ec2-18-222-203-253.us-east-2.compute.amazonaws.com www.ec2-18-222-203-253.us-east-2$
  location / {
    proxy_pass http://127.0.0.1:8080;
    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;
    proxy_redirect off;
  }
}
4
  • 1
    where is your application hosted ? EC2 ? Then check the Security Group of your EC2 instance ? Sharing the (obfuscated) URL you're trying to access would help to diagnose the issue. Sharing the exact error message and HTTP status code would help too. See stackoverflow.com/help/how-to-ask Commented Mar 12, 2019 at 9:43
  • Yes, EC2. Security group is "launch-wizard-1". Url is: ec2-18-222-203-253.us-east-2.compute.amazonaws.com . I get 404 error when try to access "/" route Commented Mar 12, 2019 at 9:58
  • 1
    Well, the name of the security group is not really useful, the port authorised is better data. But since you are receiving an HTTP answer, Security Group is OK. 404 means you are not authorised to access that path. Difficult to further debug without more details. The URL you provides sends an HTTP 502 Bad Gateway error from NGinx, which imply there is a load balancer / proxy in front of your app but this was not mentioned in the question. Commented Mar 12, 2019 at 10:03
  • 502 error occured because the server stopped running. Now I launch it permanently. Commented Mar 12, 2019 at 10:15

2 Answers 2

3

It looks like your app doesn't listen to the '/' path...

The only path you listen is the '/api' and use the router.

If you would like to get '/', try to listen '/'

app.get('/',(req,res)=>{
    //do something
});

or, I noticed in your rep, there is a app.use(express.static('dist'));, maybe you should check the path to the "dist" if your react app or something is in the 'dist' folder.

Sign up to request clarification or add additional context in comments.

Comments

2

There is a NGnix server in front of your app. This server is not correctly configured, it can not access your app. The problem is in the configuration of NGinx, not in your app.

HTTP 404 means NOT_FOUND. This error is returned by NGinx, not your app.

$ curl -v http://ec2-18-222-203-253.us-east-2.compute.amazonaws.com/
*   Trying 18.222.203.253...
* TCP_NODELAY set
* Connected to ec2-18-222-203-253.us-east-2.compute.amazonaws.com (18.222.203.253) port 80 (#0)
> GET / HTTP/1.1
> Host: ec2-18-222-203-253.us-east-2.compute.amazonaws.com
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Server: nginx/1.10.3 (Ubuntu)
< Date: Tue, 12 Mar 2019 10:21:21 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 139
< Connection: keep-alive
< X-Powered-By: Express
< Content-Security-Policy: default-src 'self'
< X-Content-Type-Options: nosniff
<
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /</pre>
</body>
</html>
* Connection #0 to host ec2-18-222-203-253.us-east-2.compute.amazonaws.com left intact

4 Comments

What may be configured incorrectly? Port or something else?
The hostname, the port. Maybe your app is binding on a specific IP address and NGinx tries localhost ... difficult to say without you sharing more details of your configuration
I provided contents of file "/etc/nginx/sites-available/default"
OK, can you SSH to your instance and verify that 127.0.0.1:8080 is available ? (curl -v http://127.0.0.1:8080)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.