4

I define my express endpoints using app.get or app.post

This seemingly works but for the root directory a trailing slash is added when the html view is returned.

The root view then becomes unavailable if enable strict routing on the app.

const express = require('express')

const app = express()

//app.enable('strict routing')

// Returns static files from the public subfolder.
// e.g. localhost:3000/dir/public/views/desktop.html
app.use('/dir', express.static('public'))

// This adds a trailing slash to localhost:3000/dir
app.get('/dir', (req, res) => require('./api/root')(req, res))

// This doesn't change and returns view on localhost:3000/dir/view
app.get('/dir/view', (req, res) => require('./api/root')(req, res))

// This returns JSON.
app.get('/dir/api/get', (req, res) => require('./api/get')(req, res))

app.listen(3000)

I tested the express.router for the root route but this didn't work either.

const router = express.Router({ strict: true })

router.get('', (req, res) => require('./api/root')(req, res))

app.use('/dir', router)

This adds a trailing slash and returns the html view without the strict:true option or crashes with the trailing slash.

Dennis Bauszus
  • 1,624
  • 2
  • 19
  • 44

0 Answers0