1

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!

dman
  • 31
  • 4
  • I've deployed Your code to DO App platform without touching any parameter and it works as expected: https://shark-app-yrgyv.ondigitalocean.app/test vs https://shark-app-yrgyv.ondigitalocean.app/ sources here: https://github.com/VOU-folks/do-app-test – num8er Nov 13 '22 at 00:36
  • Very interesting... I did as your first comment suggested and added the line of code in front of my app.get and checked the logs. When I hit {URL}/ it logs "/". It also logs "/" when I hit "/test". But if I hit a route that I didn't specify in the DO HTTP Request Routes, like {URL}/fake, it logs the "/fake" the way you would expect. – dman Nov 13 '22 at 00:43
  • try to create new DO App and deploy Your code, also You can simply fork my repo to Your account and try to deploy it too. https://github.com/VOU-folks/do-app-test – num8er Nov 13 '22 at 00:49
  • I tried what you said, used your code and created a new DO app, and was still getting the same problem. I thought I'd try just not setting any HTTP Request Routes within DO's interface, because I wasn't sure if you'd done that, and sure enough it worked. I redeployed my original code, made sure not to set any other HTTP Request Routes other than "/", and now it's working -_-. I figured that I had to set every route I listed in my express app, so I had a route for "/" and "/test" in that DO setting. Apparently that's not what it meant lol. Thank you so much! I appreciate the willingness to help – dman Nov 13 '22 at 01:28

2 Answers2

2

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: "/".

dman
  • 31
  • 4
  • that was expected, when You define in Digital ocean / and /test routes it mean it will be proxy-passed to /. / => 127.0.0.1/ and /test => 127.0.0.1/ and since You have not defined other routes - they go to / since /fake is not /test :) – num8er Nov 13 '22 at 02:01
0

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.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • You're wrong about route order, I've tested his code locally and it works in any order. Order makes sense only in `app.use` where user can define middleware. – num8er Nov 13 '22 at 00:26