skip to Main Content

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


  1. Chosen as BEST ANSWER

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


  2. Try changing the declaration of your app variable to this:

    const app = express.Router();
    
    Login or Signup to reply.
  3. I am not really good at express but try

    app.get("/page2", (request, response) => {
        response.send("./link-to-html-file");
    });
    
    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
  5. 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.

    Login or Signup to reply.
  6. Looks like you are missing the rewrite rules.

    Windows server:

    edit your web.config and set url rules are per your requirement,

    <system.webServer>
        <rewrite>
           <rules>
             <rule name="page2">
               <match url="page2/*" />
               <action type="Rewrite" url="app.js" />
             </rule>
           </rules>
         </rewrite>
    </system.webServer>
    

    Linux:

    Follow below post,

    Redirecting with htaccess to nodejs app

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search