Restful implementation of express routes do not work on the server

Asked

Viewed 474 times

0

I have a Restful API implemented in Nodejs working normally locally, with all routes responding, all right. However, when putting into production on the Kinghost server only the root route works.

I made a basic application to test alternative routes and it didn’t work either.

Follow the code below:

var express = require('express');
var app = express();

app.all("*", (req, res, next) => {
    console.log('Time:', Date.now());
    next();
});

app.get("/", (req, res) => res.send("ROOT"));
app.get("/novo", (req, res) => res.send("NOVO"));

app.listen(process.env.PORT || 21265, () => console.log("Server is running!"));

Has anyone ever had a similar problem or am I forgetting something?

EDIT: I contacted through a call with the Kinghost team and they replied that this type of service only works by adding the port to the URL. Unfortunately you have no choice

  • try putting the app.all("*",...) last

  • It did not work, ta giving the following error on the server: Cannot GET//new

  • tries to generate a project using the express-Generator, to try to see what you’re doing differently

  • It didn’t work either. I tried to raise express-Enerator application and it also doesn’t work the subroute, only the root

  • what is your host? I believe it is not a problem in the application.

  • 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

  • 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

  • Operation of nodejs applications on Kinghost, substitute process.env.PORT for process.env.PORT_APP, if your startup script is app.js, if it is the main.js, try process.env.PORT_MAIN

  • didn’t work either =/

  • Checked if routes are not case sensitive? and set to "/New" and you are sending "/new"

  • Yeah. They’re all tiny

  • Try setting the route instead of "/novo" place "novo", because the error is saying that the route //novo there is no.

  • I had already tried to do that and I was unsuccessful

Show 8 more comments

2 answers

1

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!

0

Complementing the response of @Christiano Marques, if u want to get around the problem without the possible inconveniences of manually configuring ssl certificates for your Node application and prefer to use the same ssl certificate of the Kinghost hosting, while keeping working in other environments without having to put the routes with // prefixed, you can add both forms at the same time.

For example:

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.use("/", router); // Para acesso normal sem dupla barra "//"

app.listen(process.env.PORT || 21265, () => console.log("Server is running!"));

(I searched for this answer for a long time because the manual https configuration in the application hosted on Kinghost using the same free ssl certificate of the normal Hosting was not funitioning, THANK YOU @Christiano Marques)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.