Javascript node.js中req.param中的未知值
我正在学习Javascript node.js中req.param中的未知值,javascript,node.js,parameters,Javascript,Node.js,Parameters,我正在学习node.js,因此尝试构建一个显示当前新闻的简单web应用程序。我使用的API为新闻提供了几个类别 因此,我创建了一个将类别作为参数的路由。我的路线/index.js: const router = require('express').Router(); const renderHome = require('../controllers/newsController'); const quotesCookie = require('./../middleware/quotesCo
node.js
,因此尝试构建一个显示当前新闻的简单web应用程序。我使用的API
为新闻提供了几个类别
因此,我创建了一个将类别作为参数的路由。我的路线/index.js
:
const router = require('express').Router();
const renderHome = require('../controllers/newsController');
const quotesCookie = require('./../middleware/quotesCookie');
router.get('/', quotesCookie, renderHome);
router.get('/:category', quotesCookie, renderHome);
module.exports = router;
我的controllers/newcontroller.js
如下所示:
const newsService = require('./../services/newsService');
const renderHome = async ( req, res ) => {
const category = req.params.category;
console.log(req.params);
const quote = res.quoteOfTheDay;
const { status, msg } = await newsService.topHeadlines(category);
res.render('home', {
title: 'News2Go',
author: quote.author,
quote: quote.quote,
articles: msg.articles
});
};
module.exports = renderHome;
router.get('/:category', ...)
router.get('/category/:category', ...)
例如,当我调用http://localhost:3000/entertainment
控制器中的控制台日志将此打印到控制台:
{ category: 'entertainment' }
{ category: 'sw.js' }
我完全不知道sw.js
从哪里来。。。它出现在实际类别之后几毫秒,并确保调用两次topHeadings
有人知道这是什么吗?我错过了什么吗?显然,您的网页中有一个名为sw.js
的脚本。因此,浏览器将使用URLhttp://localhost:3000/sw.js
和您的:category
路由将处理该请求并记录sw.js
类别
请记住,浏览器会请求站点上使用的所有资源,并且Express服务器会将其视为传入请求。不仅仅是顶级页面,还有所有脚本、图像、字体、CSS文件等。。。由您的页面使用
一般来说,这样定义一个完全开放的顶级路由处理程序不是一个好主意:
const newsService = require('./../services/newsService');
const renderHome = async ( req, res ) => {
const category = req.params.category;
console.log(req.params);
const quote = res.quoteOfTheDay;
const { status, msg } = await newsService.topHeadlines(category);
res.render('home', {
title: 'News2Go',
author: quote.author,
quote: quote.quote,
articles: msg.articles
});
};
module.exports = renderHome;
router.get('/:category', ...)
router.get('/category/:category', ...)
因为这会抓取所有顶级URL,而不会留下任何URL供站点的其他部分使用。使用这样的结构可能更有意义:
const newsService = require('./../services/newsService');
const renderHome = async ( req, res ) => {
const category = req.params.category;
console.log(req.params);
const quote = res.quoteOfTheDay;
const { status, msg } = await newsService.topHeadlines(category);
res.render('home', {
title: 'News2Go',
author: quote.author,
quote: quote.quote,
articles: msg.articles
});
};
module.exports = renderHome;
router.get('/:category', ...)
router.get('/category/:category', ...)
URL为http://localhost:3000/category/entertainment
。然后,您可以更清楚地将实际类别请求与站点中的所有其他请求区分开来。或者,您必须将站点上使用的所有其他URL移动到在此之前的路由和/或在其页面中使用子目录,例如:
http://localhost:3000/scripts/sw.js
http://localhost:3000/styles/main.css
显然,您的网页中有一个名为sw.js
的脚本。因此,浏览器将使用URLhttp://localhost:3000/sw.js
和您的:category
路由将处理该请求并记录sw.js
类别
请记住,浏览器会请求站点上使用的所有资源,并且Express服务器会将其视为传入请求。不仅仅是顶级页面,还有所有脚本、图像、字体、CSS文件等。。。由您的页面使用
一般来说,这样定义一个完全开放的顶级路由处理程序不是一个好主意:
const newsService = require('./../services/newsService');
const renderHome = async ( req, res ) => {
const category = req.params.category;
console.log(req.params);
const quote = res.quoteOfTheDay;
const { status, msg } = await newsService.topHeadlines(category);
res.render('home', {
title: 'News2Go',
author: quote.author,
quote: quote.quote,
articles: msg.articles
});
};
module.exports = renderHome;
router.get('/:category', ...)
router.get('/category/:category', ...)
因为这会抓取所有顶级URL,而不会留下任何URL供站点的其他部分使用。使用这样的结构可能更有意义:
const newsService = require('./../services/newsService');
const renderHome = async ( req, res ) => {
const category = req.params.category;
console.log(req.params);
const quote = res.quoteOfTheDay;
const { status, msg } = await newsService.topHeadlines(category);
res.render('home', {
title: 'News2Go',
author: quote.author,
quote: quote.quote,
articles: msg.articles
});
};
module.exports = renderHome;
router.get('/:category', ...)
router.get('/category/:category', ...)
URL为http://localhost:3000/category/entertainment
。然后,您可以更清楚地将实际类别请求与站点中的所有其他请求区分开来。或者,您必须将站点上使用的所有其他URL移动到在此之前的路由和/或在其页面中使用子目录,例如:
http://localhost:3000/scripts/sw.js
http://localhost:3000/styles/main.css
也许您有一个3000端口的服务工作者(sw.js)注册。请在“应用程序/服务工作者”选项卡中查看Chrone的开发工具。您的权利,谢谢@Gillespie59。也许您有3000端口的服务工作者(sw.js)注册。请在“应用程序/服务人员”选项卡中查看Chrone的开发工具。您的权利,谢谢@Gillespie59。谢谢@jfriend00的解释。我改变了路线,效果很好。谢谢@jfriend00的解释。我改变了路线,效果很好。