Home > database >  Node.js Express only working for home page
Node.js Express only working for home page

Time:09-30

I am running Node.js on Plesk Obsidian 18.0.37. I am not able to follow the link to page 2, only able to visit the home page. If I do follow the link or manually go to /page2, I'll get a 404 response. How could this be happening?

app.js:

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

app.get("/", (request, response) => {
    response.send("<h1>Home page</h1>"   
                  '<br><a href="/page2">Page 2</a>');
});

app.get("/page2", (request, response) => {
    response.send("<h1>Page 2</h1>");
});

app.listen(process.env.PORT);

I also tried:

serving a file, as suggested by Aqua

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

app.get("/", (request, response) => {
    response.send("<h1>Home page</h1>"  
                  '<br><a href="/page2">Page 2 with sendFile</a>');
});

app.get("/page2", (request, response) => {
    response.sendFile('page2.html');
});

app.listen(process.env.PORT);

using a router, as suggested by Harshit Rastogi

const express = require('express');
const app = express();
var router = express.Router();

router.get("/", (request, response) => {
    response.send("<h1>Home page with router</h1>"  
                  '<br><a href="/page2">Page 2</a>');
});

router.get("/page2", (request, response) => {
    response.send("<h1>Page 2 with router</h1>");
});

app.use(router);
app.listen(process.env.PORT);

I am still not able to follow the link to page 2, only able to visit the home page.

CodePudding user response:

Try changing the declaration of your app variable to this:

const app = express.Router();

CodePudding user response:

I am not really good at express but try

app.get("/page2", (request, response) => {
    response.send("./link-to-html-file");
});

CodePudding user response:

Did you enable nginx reverse proxy?

How to install and enable nginx reverse proxy on a Plesk?

Every code that you posted worked for me. Your error seem something related with port and/or proxy.

CodePudding user response:

I have copied your app.js file and all is working for me, macOS.

please see this post : https://talk.plesk.com/threads/webpages-are-no-longer-reachable-after-update-from-17-8-to-18-0.359321/

from what i understand this was fixed in the next version 18.0.38 or apply the fix they are working on for version 18.0.37.

  • Related