首先,我是Node的初学者,所以我的问题肯定是基本的。所以,我通过一个网站学习Node,并且我在一个Express练习中被阻止了。我需要创建一个接受URL路径中的动态参数的路由,并使用适当的作者使用quote(对象在代码中查看)进行响应。 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);
答案 0 :(得分:2)
请尝试使用此处阻止的行:
response.send(quotes[request.params.name]);
这取决于:name
是否正确,因此您可能希望首先使用quotes
检查它是否存在于您的Object.hasOwnProperty()
对象中。