Home > front end >  Node/Express Server on DigitalOcean only responding with "/" route even if I hit "/te
Node/Express Server on DigitalOcean only responding with "/" route even if I hit "/te

Time:11-13

I'm trying to host a Node.js/Express server for the first time, and I'm currently attempting to use Digital Ocean's App Platform. I have it up and running, but my routes aren't working correctly. Digital Ocean allows me to set HTTP Request Routes, and I have "/" and "/test" set for two of them. No matter what route I hit, it only ever responds with the "/" route.

Here is my code for reference:

require("dotenv").config();
const { SERVER_PORT } = process.env;

const express = require("express");
const cors = require("cors");

const app = express();

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

app.get("/", (req, res) => {
  res.status(200).send("This is the / route")
})

app.get("/test", (req, res) => {
  res.status(200).send("This the /test route");
})

app.listen(SERVER_PORT, () => {
  console.log(`Server running on port ${SERVER_PORT}`);
});

When I make a GET request to "https://{DigitalOceanURL}/", I get the correct response of "This is the / route", however when I make a GET request to "https://{DigitalOceanURL}/test", I still get the response of "This is the / route" and not "This the /test route" as is in my code.

If I try and make request to a route that doesn't exist, however, such as "https://{DigitalOceanURL}/doesntexist" I get the response "Cannot GET /doesntexist". To me this suggests that the routes I'm entering into DigitalOcean are registering, but for some reason it's not seeing anything past the initial "/" in the route.

I've been trying to Google for a while now and I can't seem to diagnose what's wrong. I thought it might be something with app.use vs app.get, as on this answer on a SO post it says that app.use "...will match all requests beginning with..." your route, whereas app.get "...matches only GET request with exact match.". So it seems that, since I'm hitting the exact route, it should be working? But it feels like the general area of my problem, as it seems to only be registering the "/" in the route and nothing else?

Both the endpoints work when running the server on localhost, so it seems like there must be some disconnect with DigitalOcean.

Any help would be greatly appreciated - as I mentioned this is my first time trying to host and it's a bit overwhelming.

Thank you!

CodePudding user response:

The issue was with my Digital Ocean settings. As I said in my question: "Digital Ocean allows me to set HTTP Request Routes, and I have "/" and "/test" set for two of them." It worked when I removed the "/test" route and only had one, "/" in my Digital Ocean settings. I interpreted it as "List all routes I had in my express app", but I guess that's not what it meant. There only should've been one set: "/".

CodePudding user response:

Your / route declaration comes before the one for /test. Express matches in code order, so / picks up every incoming request, and none make it to the more specific route. Switch their order.

This isn't specific to Digital Ocean, of course.

  • Related