【发布时间】:2020-05-15 05:18:55
【问题描述】:
早安,
我很惊讶我找不到任何关于 next 包的 getRequestHandler 和 render 函数的信息。
我正在尝试设置自定义服务器,并且想知道 render 函数实际上在做什么,或者为什么要使用它? getRequestHandler 清楚地呈现了应用程序,那么我为什么要使用 render 手动传递路径?另外,分别传入路径名和查询有什么意义?
我显然对这两个的用例感到困惑 - 在哪种情况下我会使用其中一个?
感谢大家的帮助。
阿纳尼
见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')
})
})
【问题讨论】:
标签: javascript node.js next.js