Custom server problem
Unanswered
Dwarf Crocodile posted this in #help-forum
Dwarf CrocodileOP
I'm using a custom Node.js server to run my development environment as described in the Next.js documentation: https://nextjs.org/docs/pages/building-your-application/configuring/custom-server
I am trying to integrate @edge-csrf/nextjs, which requires adding their functionalities to a middleware file: https://github.com/kubetail-org/edge-csrf/tree/main/packages/nextjs
Since I'm using a custom server, I have my middleware within it and have added the package's functionality as shown in the example below. Unfortunately, my project is throwing errors and nothing is working. Can anyone provide some guidance?
Thanks
})
})`
I am trying to integrate @edge-csrf/nextjs, which requires adding their functionalities to a middleware file: https://github.com/kubetail-org/edge-csrf/tree/main/packages/nextjs
Since I'm using a custom server, I have my middleware within it and have added the package's functionality as shown in the example below. Unfortunately, my project is throwing errors and nothing is working. Can anyone provide some guidance?
Thanks
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const hostname = 'localhost'
const port = 3000
// when using middlewarehostnameandportmust be provided below
const app = next({ dev, hostname, port })
const handle = app.getRequestHandler()
const csrf = require("@edge-csrf/nextjs")
// Initialize CSRF protection middleware
const csrfMiddleware = csrf.createCsrfMiddleware({
cookie: {
secure: true
},
})
app.prepare().then(() => {
createServer(async (req, res) => {
try {
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
// Apply CSRF middleware
await csrfMiddleware(req, res)
if (pathname === '/a') {
await app.render(req, res, '/a', query)
} else if (pathname === '/b') {
await app.render(req, res, '/b', query)
} else {
await handle(req, res, parsedUrl)
}
} catch (err) {
console.error('Error occurred handling', req.url, err)
res.statusCode = 500
res.end('internal server error')
}
})
.once('error', (err) => {
console.error(err)
process.exit(1)
})
.listen(port, () => {
console.log(> Ready on http://${hostname}:${port}`)})
})`