首先,我是 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
对象中
关于javascript - URL 中带有动态参数的快速路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26798251/