Search code examples
node.jsexpressplesk

Node.js Express only working for home page


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.


Solution

  • Managed to solve this by switching from Windows Server to a Linux server.