Creating routing for a static site using Express

I am creating a static site, (HTML, CSS, JS) using nodeJS and express for the server...

I am trying to create routes, so that the user sees /about, as opposed to about.html... For the main, home page (home.html), it works fine. However, when trying to access the pages using the defined routes via app.get, I keep getting errors - any pointers...

Code is as follows - my styling and JS files ate in the public directory and the HTML files are in the views directory :

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

app.use(express.static(path.join(__dirname + '/public')));
app.use(express.static(path.join(__dirname + '/views')));

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/views/home.html');

app.get('/about', (req, res) => {
  res.sendFile(__dirname + '/views/about.html');

app.get('/contact', (req, res) => {
  res.sendFile(__dirname + '/views/contact.html');

// app.use(express.static('public'));
// app.use(express.static('views'));
// console.log(__dirname);
module.exports = app;

The error I get is :

Cannot GET /views/contact.html

Read more here:

Content Attribution

This content was originally published by Samuel Adjei at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: