How do I add multiple pages in Node JS+Express

Issue

I have followed this tutorial on Node JS for RPI (https://www.youtube.com/watch?v=QdHvS0D1zAI) and was wondering how I could add multiple websites to my web app. Whenever I change my page on localhost (localhost:5000/page2) it works just fine, and when I start Nginx and make the app public, the root page works but when I change my page (myip/page2), I get an error 404

const { readFileSync, writeFileSync } = require(`fs`);

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




app.get(`/`, (req, res) => {
    const count = readFileSync(`/home/pi/Desktop/database.txt`, `utf-8`);
    console.log(`count`, count)

    const newCount = parseInt(count) + 1
    //const newCountString = String.format(newCount)
    

    writeFileSync(`/home/pi/Desktop/database.txt`, newCount.toString());

    res.send(`
    <!DOCTYPE ,html>
    <html lang="en">
    <head>
        <meta charset="utf-8"/>
        <meta name="viewport" content="width=device-width, initial-scale1" />
        <title>Test Website</title>
    </head>
    <body>
        <h1>Test Website</h1>
        <h1>Index</h1>
        <p>This page has been loaded ${newCount} times</p>
        <h1>Page 2:</h1>
        <a href="/page2">Link to Page 2</a>
        
    </body>
    </html>
    `);
});
app.get(`/page2`, (req, res) => {
    //res.sendFile(path.join(__dirname, '/resources/page2.html'));
    res.send("hello page 2")
});
app.listen(5000, () => console.log('http://localhost:5000/'));

I am very new to all this and understand it has something to do with routing, but I would like to have an answer that works with what I have here.
Thanks.
EDIT: I looked into my Nginx config and I have this:

server {
    listen 80;
    listen [::]:80;

    server_name _;

    root /var/www/html;
    #index index.html;

    location / {
        proxy_pass http://localhost:5000;
        try_files $uri $uri/ =404;
    }
}

What would I need to change?

Solution

The issue was I didn’t route the pages to the Nginx config, I had to add this:

location /page2 {
proxy_pass http://localhost5000/page2;
}

Answered By – narwhal_tamer

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published