简体   繁体   English

使用具有多个URL和可选参数的express.Router()

[英]Using express.Router() with multiple urls and optional parameters

I am having trouble using express.Router() the way I wan to and am not sure what my issue is. 我在使用express.Router()时遇到麻烦,不确定我的问题是什么。 My problem is that when I make a GET request to the url '/api/zip' or to '/api/zip/' + zip, where zip is a numerical zip code, the only route that gets hit is '/', which in the example below logs '$$$$$$$$$$$$'. 我的问题是,当我对url'/ api / zip'或'/ api / zip /'+ zip发出GET请求时,其中zip是数字邮政编码,唯一被点击的路由是'/',在下面的示例中记录为“ $$$$$$$$$$$$”。

When I take out the route for '/' in results_api.js, 'made it to server.js' gets logged, but I don't get anything from results_api.js. 当我在results_api.js中取出“ /”的路由时,会记录“使其到达server.js”,但从results_api.js中什么也没得到。 Let me know if I need to post more context code. 让我知道是否需要发布更多上下文代码。 I am using an Angular front end, but I am assuming that since my request always gets to server.js (verified by 'made it to server.js' being logged), that my problem is within results_api.js. 我使用的是Angular前端,但是我假设由于我的请求总是到达server.js(已通过记录“将其发送到server.js”进行验证),所以我的问题出在results_api.js中。

I have used this routing method before and am not sure what is going wrong this time around. 我以前使用过这种路由方法,但不确定这次出了什么问题。 How do I manage to hit routes.get('/zip/:zip... ? 我如何设法打routes.get('/ zip /:zip ...?

In my server.js file: 在我的server.js文件中:

var express = require('express');
var routes = express.Router();

//route to index.html
var assetFolder = Path.resolve(__dirname, '../client/');
routes.use(express.static(assetFolder));

//api routes
routes.use('/api/*', require(‘./api/results_api.js’), function(){ 
console.log('made it to server.js')});

My results_api.js file: 我的results_api.js文件:

var router = require('express').Router();

router.get('/', function(req, res){
  console.log('$$$$$$$$$$$$$$$')

}); 

router.get('/zip', function(req, res){
  console.log(‘###################’)

}); 

router.get('/zip/:zip', function(req, res){
  console.log('%%%%%%%%%%%%%%%%%')

}); 

module.exports = router;

This question: 这个问题:

express.Router() get url route with optional parameters express.Router()获取带有可选参数的URL路由

Is close to mine, but there is no intermediate parameter ( in my case '/zip/' after '/api'). 离我的很近,但是没有中间参数(在我的情况下是“ / api”之后的“ / zip /”)。 Is this indicative of what I am trying to do just being the wrong way to go about building my api? 这是否表明我正在尝试做的事情只是构建API的错误方法?

Call these handlers in reverse order. 以相反的顺序调用这些处理程序。 /zip/:zip , then /zip , then / . /zip/:zip ,然后是 /zip ,然后是 /

Actually, the problem is this line: 实际上,问题是此行:

routes.use('/api/*', require(‘./api/results_api.js’), function(){ 
console.log('made it to server.js')});

/api/* will "gobble up" anything after /api/ , so your router will think it is called with / . /api/*将“吞噬” /api/之后的所有内容,因此您的路由器会认为它被/调用。 Change this to: 更改为:

routes.use('/api', require('./api/results_api.js'), function() { 
    console.log('made it to server.js')
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM