【问题标题】:Getting a 404 when I click on the answer anchor link当我点击答案锚链接时得到 404
【发布时间】:2018-03-12 19:58:52
【问题描述】:

当我点击 card.pug 文件中的答案链接时,我收到了 404。

extends layout.pug

block content
section#content
h2= text
if hint
  p
    i Hint: #{hint}
 a(href=`${id}?side=${sideToShow}`)= sideToShowDisplay

card.pug 文件的路由文件如下:

const express = require('express');
const router = express.Router();
const { data } = require('../data/flashcardData.json');
const { cards } = data;

router.get('/:id', (req, res) => {
const { side } = req.query;
const { id } = req.params;
const text = cards[id][side];
const { hint } = cards[id];

const templateData = { id, text };

if (side === 'question') {
templateData.hint = hint;
templateData.sideToShow = 'answer';
templateData.sideToShowDisplay = 'Answer';
} else if (side === 'answer') {
templateData.sideToShow = 'question';
templateData.sideToShowDisplay = 'Question';
}

res.render('card', templateData);
});

module.exports = router;

我不确定为什么会这样。任何帮助将非常感激。它从数据文件夹中的 JSON 文件中提取数据。

【问题讨论】:

  • 首先,找出问题出在客户端还是服务器。链接实际上重定向到哪里?这是正确的网址吗?如果是这样,则忽略 pug 文件并将其视为有关 express 代码的问题。如果不是,那就是pug文件的问题了。
  • 我注意到它在查询字符串中重复了 id。 localhost:3000/cards/4/4?side=answer 。如您所见,问题是 id 在 ?标记。
  • 看来问题出在客户端:id 应该是 /cards/4 而不是 /cards/4/4。你能显示layout.pug 的调用地址吗?
  • doctype html html(lang="en") head title flash Cards body include includes/header.pug block content include includes/footer.pug
  • 关于如何解决这个大卫的任何想法?

标签: javascript express routing


【解决方案1】:

你可以使用锚href = ' user single code here'

例子

a(href=`${id}?side=${sideToShow}`)= sideToShowDisplay

下面用上面代替

a(href='${id}?side=${sideToShow}')= sideToShowDisplay

对我有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 2013-07-26
    • 1970-01-01
    相关资源
    最近更新 更多