How do I add multiple pages in Node JS+Express


I have followed this tutorial on Node JS for RPI ( 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());

    <!DOCTYPE ,html>
    <html lang="en">
        <meta charset="utf-8"/>
        <meta name="viewport" content="width=device-width, initial-scale1" />
        <title>Test Website</title>
        <h1>Test Website</h1>
        <p>This page has been loaded ${newCount} times</p>
        <h1>Page 2:</h1>
        <a href="/page2">Link to Page 2</a>
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.
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?


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