点击card.pug内答案链接触发404错误,寻求技术排查帮助
Hey there, let's break down why you're hitting that 404 error when clicking the answer link in your card.pug file. Since you mentioned the data pulls from a JSON file in the data folder, here are the most common issues to troubleshoot step by step:
1. Double-check the link path in card.pug
First, inspect the actual link generated in your rendered HTML. Right-click the link in your browser and select "Inspect" to see what the href attribute is set to.
- If you're pulling a path/ID from your JSON data (like
a(href='/answers/' + item.answerId) View Answer), make sure the concatenated path matches exactly what your routes are expecting. - Watch out for missing slashes or extra segments—for example, if your route is
/answers/:id, a link to/answer/123(missing the 's') will throw a 404.
2. Validate your route configuration
Even without seeing your route file, let's cover the key checks for typical setups (like Express.js, which is common with Pug):
- Ensure your route defines the correct path. If your link points to
/answers/456, your route should look something likerouter.get('/answers/:id', (req, res) => { ... }). - Confirm the route is properly mounted in your main app file. For example, if your routes are in
routes/answers.js, you need a line likeapp.use('/answers', require('./routes/answers'))inapp.js—a wrong mount path here will break all requests to that route.
3. Check your JSON data for errors
Open the JSON file in your data folder and verify the values you're using for the answer links:
- If you're using an ID parameter, make sure the
answerId(or whatever field you're using) exists for every entry and doesn't have typos. - If the JSON stores full paths, ensure those paths align with your server's route structure—outdated or incorrect paths here will lead directly to 404s.
4. Watch for case sensitivity and typos
This is a super common culprit, especially on Linux-based servers:
- Route paths are case-sensitive. A link to
/Answers/123won't match a route defined as/answers/:id. - Double-check for misspellings (e.g.,
answrinstead ofanswer) in both your Pug template and route definitions.
5. Test the route directly
Skip the card link entirely and type the expected route URL directly into your browser's address bar (like http://localhost:3000/answers/1).
- If this also returns a 404, the problem is definitely in your route setup or server configuration.
- If it works, the issue is in how you're generating the link in
card.pug.
If you can share snippets of your route code, the link section from card.pug, or a sample of your JSON data, we can narrow this down even further.
内容的提问来源于stack exchange,提问作者AltBrian

