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.
6
Answers
Managed to solve this by switching from Windows Server to a Linux server.
Try changing the declaration of your
app
variable to this:I am not really good at express but try
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.
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.
Looks like you are missing the rewrite rules.
Windows server:
edit your web.config and set url rules are per your requirement,
Linux:
Follow below post,
Redirecting with htaccess to nodejs app