我正在尝试将 :id slug 添加到我下一个.js应用程序中博客文章的 url 中。 如果我只是按照从应用程序到页面的链接进行操作,它可以工作,但是如果我刷新帖子上的页面或直接访问页面,我会不断收到以下错误:
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object
at assertPath (path.js:39:11)
at extname (path.js:835:5)
at new View (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibview.js:56:14)
at Function.render (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibapplication.js:570:12)
at app.get (E:Web PagesReact AppsFull StackNextjs-Blogserver.js:31:16)
at Layer.handle [as handle_request] (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibrouterlayer.js:95:5)
at next (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibrouterroute.js:137:13)
at Route.dispatch (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibrouterroute.js:112:3)
at Layer.handle [as handle_request] (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibrouterlayer.js:95:5)
at E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibrouterindex.js:281:22
at param (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibrouterindex.js:354:14)
at param (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibrouterindex.js:365:14)
at Function.process_params (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibrouterindex.js:410:3)
at next (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibrouterindex.js:275:10)
at jsonParser (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesbody-parserlibtypesjson.js:110:7)
at Layer.handle [as handle_request] (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibrouterlayer.js:95:5)
at trim_prefix (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibrouterindex.js:317:13)
at E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibrouterindex.js:284:7
at Function.process_params (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibrouterindex.js:335:12)
at next (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibrouterindex.js:275:10)
at expressInit (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibmiddlewareinit.js:40:5)
at Layer.handle [as handle_request] (E:Web PagesReact AppsFull StackNextjs-Blognode_modulesexpresslibrouterlayer.js:95:5)
我已经阅读了文档 下一个带链接的Js Docs,并且完全按照描述进行操作。
这是我的代码:
服务器.js
nextApp.prepare().then(() => {
const app = express();
app.use(express.json());
app.get('/post/:id', (req, res) => {
return app.render(req, res, '/post', { id: req.params.id });
});
app.get('*', (req, res) => {
return handle(req, res);
});
app.listen(PORT, err => {
if (err) throw err;
console.log(`ready at http://localhost:${PORT}`);
});
});
链接到页面
<Link as={`/post/${post._id}`} href={`/post?id=${post._id}`}>
<a>Link text</a>
</Link>
根据文档,这就是它应该如何写出来。 我不确定我错过了哪些步骤,或者 webpack 或其他东西是否存在问题。 如果您需要更多信息,请告诉我任何帮助将不胜感激。
这里的目标是将请求传递给 NextJS,而不是使express
应用程序呈现。
将渲染更改为:
app.get('/post/:id', (req, res) => {
return nextApp.render(req, res, '/post', { id: req.params.id });
});
例