[英]GET request parameters with koa-router
http://localhost:3000/endpoint?id=83結果為 404(未找到)。 所有其他路線都按預期工作。 我在這里錯過了什么嗎?
router
.get('/', function *(next) {
yield this.render('index.ejs', {
title: 'title set on the server'
});
})
.get('/endpoint:id', function *(next) {
console.log('/endpoint:id');
console.log(this.params);
this.body = 'Endpoint return';
})
koa-router 參數文檔
//Named route parameters are captured and added to ctx.params.
router.get('/:category/:title', function *(next) {
console.log(this.params);
// => { category: 'programming', title: 'how-to-node' }
});
角度控制器中的請求:
$http.get('/endpoint', {params: { id: 223 }})
.then(
function(response){
var respnse = response.data;
console.log(response);
}
);
你的參數格式不對
用這個替換你的路線
.get('/endpoint/:id', function *(next) {
console.log(this.params);
this.body = 'Endpoint return';
})
.get('/endpoint/', function *(next) {
console.log(this.query);
this.body = 'Endpoint return';
})
.get('/endpoint/:id', function *(next) {
console.log(this.params);
this.body = 'Endpoint return';
})
也許為時已晚,但對於那些還遇到這個問題的人來說,不是關鍵字this,而是ctx。 以下,當與url協商時
.get('/endpoint/:id', async (ctx, next) => {
console.log(ctx.params);
this.body = 'Endpoint return'; })
返回以下 json:
{ "id": "45"}
和這個:
.get('/endpoint/:id', async (ctx, next) => {
console.log(ctx.params.id);
this.body = 'Endpoint return'; })
當使用相同的 url 進行咨詢時返回
45
編輯:好消息是這兩個端點確實不同。 您可以擁有兩個端點,路由器可以根據您在瀏覽器中輸入的 url 在兩者之間做出決定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.