在URL中显示烤肉串案件文本,但实际上将请求发送到不可辨认的ID

发布于 2025-02-02 17:57:58 字数 843 浏览 4 评论 0原文

当用户单击“阅读更多”时,浏览器将转到带有这样的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情独悲 2025-02-09 17:57:58

您可以继续使用params和 id 键,也可以将其更改为主题。只需确保主题是独一无二的即可;在主题字段上添加唯一约束到数据库表(或在使用mongo的情况下收集),

您可以轻松地将kebab案例(lorem-ipsum)转换为普通字符串(lorem ipsum)

router.get("/gp/:topic", ensureAuth, async (req, res) => {

    // convert the kebab case to normal string (lorem-ipsum -> lorem ipsum)
    let topicString = req.params.topic.replace('-',' ');

    // We want to ignore capitalization to match the title, so use regex as shown below
    try {
        ab = await XXX.find({
            title: { $regex: new RegExp(`^${topicString}

您可以继续使用params和 id 键,也可以将其更改为主题。只需确保主题是独一无二的即可;在主题字段上添加唯一约束到数据库表(或在使用mongo的情况下收集),

您可以轻松地将kebab案例(lorem-ipsum)转换为普通字符串(lorem ipsum)

, 'i') }, status: "public", }).lean(); res.render("aFile", { ab, title: ab[0].title, }) } catch (error) { console.error(error); res.render("/error/500");} });

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)

router.get("/gp/:topic", ensureAuth, async (req, res) => {

    // convert the kebab case to normal string (lorem-ipsum -> lorem ipsum)
    let topicString = req.params.topic.replace('-',' ');

    // We want to ignore capitalization to match the title, so use regex as shown below
    try {
        ab = await XXX.find({
            title: { $regex: new RegExp(`^${topicString}

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)

, 'i') }, status: "public", }).lean(); res.render("aFile", { ab, title: ab[0].title, }) } catch (error) { console.error(error); res.render("/error/500");} });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文