Good day,
I was surprised that I couldn't find any information on the getRequestHandler and render functions of the next package.
I am trying to set up a custom server and was wondering what the render function was actually doing or why it is even used? getRequestHandler clearly renders the app so why would I ever want to use render to pass in a path manually? Also, what is the point of passing in pathname and query separately?
I am clearly confused regarding the use cases of these two - in which situation would I use one or the other?
Thank you for everyone's help.
Anagni
See https://nextjs.org/docs/advanced-features/custom-server
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
app.prepare().then(() => {
createServer((req, res) => {
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
if (pathname === '/a') {
app.render(req, res, '/b', query)
} else if (pathname === '/b') {
app.render(req, res, '/a', query)
} else {
handle(req, res, parsedUrl)
}
}).listen(3000, err => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})