[英]Express route with dynamic arguments in the URL
首先,我是Node的初學者,因此我的問題肯定是基本的。 因此,我在一個網站上學習Node,而我在一個Express練習上受阻。 我需要創建一個路由,該路由在URL路徑中接受動態參數,並使用適當作者的引號(對象參見代碼后)進行響應。 js如下所示:
var express = require('express');
var app = express();
var quotes = {
'einstein': 'Life is like riding a bicycle. To keep your balance you must keep moving',
'berners-lee': 'The Web does not just connect machines, it connects people',
'crockford': 'The good thing about reinventing the wheel is that you can get a round one',
'hofstadter': 'Which statement seems more true: (1) I have a brain. (2) I am a brain.'
};
app.get('/quotes/:name', function(request, response){
response.send(request.params.name); /*And i'm blocked here*/
});
app.listen(8080);
嘗試此操作以防止阻塞:
response.send(quotes[request.params.name]);
但這是基於:name
條件,因此您可能需要首先使用Object.hasOwnProperty()
檢查它是否存在於quotes
對象中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.