在URL中显示烤肉串案件文本,但实际上将请求发送到不可辨认的ID
当用户单击“阅读更多”时,浏览器将转到带有这样的URL的页面=> https://example.com/stample.com/stample.com/stample/629217f17f17070707070717debfff9f9f9f9ff029 datab )。说这个故事的话题是“ lorem ipsum”,我希望URL显示 https://示例。 com/story/lorem-ipsum 不是前一个。问题是我使用req.params.ID获得了故事ID,然后呈现页面。有什么办法可以显示后者具有与前者相同的功能?
router.get("/gp/:Id", ensureAuth, async (req, res) =>{try {
ab = await XXX.find({
id: req.params.Id,
status: "public",
}).lean();
res.render("aFile", {
ab,
title: ab[0].title,
})}catch (error) {
console.error(error);
res.render("/error/500");}});
When the user clicks on 'read more', the browser goes to a page with a url like this=>
https://example.com/stories/629217f1e70717debf9ff029 (the last parameter is the object id in the database). Say the topic of the story is 'Lorem ipsum', I want the url to show https://example.com/stories/lorem-ipsum and not the previous one. Problem is that I get the story id with req.params.id and then render the page. Is there any way I can show latter with the same functionality as the former?
router.get("/gp/:Id", ensureAuth, async (req, res) =>{try {
ab = await XXX.find({
id: req.params.Id,
status: "public",
}).lean();
res.render("aFile", {
ab,
title: ab[0].title,
})}catch (error) {
console.error(error);
res.render("/error/500");}});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以继续使用params和 id 键,也可以将其更改为主题。只需确保主题是独一无二的即可;在主题字段上添加唯一约束到数据库表(或在使用mongo的情况下收集),
您可以轻松地将kebab案例(lorem-ipsum)转换为普通字符串(lorem ipsum)
You can continue using the params and the id key or change it to topic. Just make sure the topic is unique; add a unique constraint on the topic field to the database table (or collection in case you are using mongo)
You can easily convert kebab case (lorem-ipsum) to normal string (lorem ipsum)