Why is my Express endpoint /api/test not responding as expected?

I’m having trouble with my Express server. I set up a simple test endpoint at /api/test, but it’s not working right. When I try to access it, I get a 404 error. Here’s what I did:

const app = express();
app.use(cors());
app.use(express.json());

app.get('/api/test', (req, res) => {
  res.json({ msg: 'Server is up!' });
});

app.listen(5002, '0.0.0.0', () => {
  console.log('Server running on port 5002');
});

The main page works fine, but /api/test doesn’t. I’ve tried using the browser, Postman, and curl, but no luck. My React app can’t connect either. It gives me a ‘Network Error’ when I try to use axios.

I’ve already:

  • Added CORS
  • Listened on all network interfaces
  • Cleared my browser cache
  • Restarted everything
  • Double-checked my API URL

What am I missing? Why isn’t my endpoint working? And how can I fix the connection issue with my React app? Any help would be great!

Have you checked your server logs? Sometimes the issue isn’t in the code itself, but in how the server is running. Make sure your server is actually starting up without errors. Also, double-check the port number you’re using to access the endpoint. If you’re running the React app on a different port, you might need to set up a proxy in your package.json file.

Another thing to consider is your firewall settings. Sometimes they can block connections, especially if you’re trying to access the server from a different device. You might want to temporarily disable your firewall to test if that’s the issue.

Lastly, try adding a wildcard route to catch all requests and log them. This can help you see if the request is even reaching your server:

app.use('*', (req, res, next) => {
  console.log('Request received:', req.method, req.originalUrl);
  next();
});

This should give you more visibility into what’s happening when you try to access the endpoint.

I’ve encountered similar issues before, and one thing that often gets overlooked is the order of middleware and route definitions. Express processes routes in the order they’re defined, so make sure your ‘/api/test’ route is placed before any catch-all routes or error handlers.

Another potential culprit could be your network configuration. If you’re running this on a local network or a VPS, ensure that the correct ports are open and forwarded. I once spent hours debugging only to realize my router was blocking the port.

Have you tried using a tool like Wireshark to capture the network traffic? It can provide valuable insights into what’s happening at the packet level. This helped me diagnose a tricky CORS issue that wasn’t apparent from server logs alone.

Lastly, double-check your React app’s environment variables. I’ve been bitten by using the wrong API URL in production builds because I forgot to update the .env file. A simple console.log of the API URL in your React app might reveal the problem.

hey, have u tried checking ur network settings? sometimes the issue is with the network, not the code. make sure ur firewall isn’t blocking the connection. also, try running the server on a different port, like 3000 or 8080. that’s helped me before when i had similar probs.