I am trying to render the static pages in my public folder but when I try and render the pagenotfound middleware comes up and when I remove the middleware, I keep getting the following error
Cannot GET /
this is my code in my app.js
const express = require('express')
const app = express()
const signup = require('./routers/sign-up')
const pagenotfound = require('./middleware/pagenotfound')
// middleware
app.use(express.static('./public'))
app.use(express.json())
app.use('/api/v1/', signup)
app.use(pagenotfound)
const port = process.env.PORT || 3000
const start = async () => {
try {
app.listen(port, console.log(`Server is listening on port ${port}...`))
} catch (error) {
console.log(error)
}
}
start()
the structure of the public folder is
Index.html
css folder
javascript frontend
CodePudding user response:
The static
middleware looks for files relative to the folder where the node
process runs, so your code works only if that contains the public
folder. Specify the full path to the public folder, for example,
app.use(express.static(__dirname '/public'))
if the public
folder is in the same directory as app.js
.