Search code examples
htmlexpressnginxforever

Adding another page other than index.html


I started my own VPS, and started a web server using NGINX. I have an index.html. How do I create other pages, like an about page, and having it live at www.my-domain-name.com/about/

Does this mean I have to edit my app.js file, if so, how?

Amendment: I added Lazar's suggested amendment to the Express code to get the about.html.

'use strict';

const express = require("express");
const app = express();

// Static css/js files
app.use('/static', express.static('./dist'));

app.get("/", function(req, res) {
  res.sendFile( __dirname + '/index.html');
});

app.get("/about", function(req, res) {
    res.sendFile( __dirname + '/about.html');
});

const port = 3001;

// Start server
app.listen(port, function() {
  console.log("Listening on " + port);
});

In index.html, the link to the about page is:

<a href="/about.html">About me.</a>

Both /about and /about.html don't currently work and receive the error message: Cannot GET /about.html

Edit: I am using forever, so I had to forever restartall


Solution

  • Considering that you are using express, for every created route, create appropriate html page - or use some other thing, like handlebars, etc.

    For example you created index.html for "/" route. For about us route, create aboutus.html

    app.get("/aboutus", function(req, res) {
      res.sendFile( __dirname + '/aboutus.html');
    });
    

    and so on...

    For more info check their official web page: https://expressjs.com/