I also went through this problem in Kinghost, my solution was to use a hack in the api to make work the sub-routes and make use of the Express Router.
I noticed that in the requests in Kinghost, an extra bar is added in the subroutes, see error message Cannot GET//new and not the default that would be Cannot GET/new, so if you do a GET on /test this request arrives in the api as //test, but this only for sub-routes from the root, not to the root. Ex: if the application in the Kinghost panel is mapped to https://domain.com/api (my case), everything that arrives in /api/test will arrive in the application as //test. That’s why a "/test" route does not work, if you put all your routes as "//route", it works, but there is no need to refactor the entire application for this.
The cat jump was, if you map a main route with the "//" for all your routing (hence the use of the Express Router) all come to work, including the root, because you will put all your routes under the same pseudo endpoint, the "//".
Adapting from your example would look like this:
var express = require('express');
var app = express();
var router = express.Router(); // USO DO ROUTER
app.all("*", (req, res, next) => {
console.log('Time:', Date.now());
next();
});
// ROTAS NO ROUTER E NÃO NO APP
router.get("/", (req, res) => res.send("ROOT"));
router.get("/novo", (req, res) => res.send("NOVO"));
app.use("//", router); // PSEUDO ROTA PRINCIPAL COMO "//" PARA TODO O ROUTER
app.listen(process.env.PORT || 21265, () => console.log("Server is running!"));
I know your question is 1 year old, but I hope you can help others who have the same problem as us.
EDIT: I got in touch through a call with the Kinghost team and they told me that this kind of service only works by adding the port to the URL. Unfortunately you have no choice
Reading this and knowing the real problem, only proves that these supports know nothing, rssss!
try putting the app.all("*",...) last
– Tobias Mesquita
It did not work, ta giving the following error on the server: Cannot GET//new
– Paulo Augusto
tries to generate a project using the express-Generator, to try to see what you’re doing differently
– Tobias Mesquita
It didn’t work either. I tried to raise express-Enerator application and it also doesn’t work the subroute, only the root
– Paulo Augusto
what is your host? I believe it is not a problem in the application.
– Tobias Mesquita
Kinghost. Yeah, I thought so too. I just got in touch with the support and they said the server is normal. Funny that if I put the server port in the URL, it works
– Paulo Augusto
I could even use the API using the port in the URL, but I need the url with https and when adding the port I can’t access
– Paulo Augusto
Operation of nodejs applications on Kinghost, substitute
process.env.PORT
forprocess.env.PORT_APP
, if your startup script isapp.js
, if it is themain.js
, tryprocess.env.PORT_MAIN
– Tobias Mesquita
didn’t work either =/
– Paulo Augusto
Checked if routes are not case sensitive? and set to "/New" and you are sending "/new"
– Leandro Angelo
Yeah. They’re all tiny
– Paulo Augusto
Try setting the route instead of
"/novo"
place"novo"
, because the error is saying that the route//novo
there is no.– NoobSaibot
I had already tried to do that and I was unsuccessful
– Paulo Augusto