Javascript 需要解释node.js npm(express)吗?
请给我解释一下这条线是怎么工作的Javascript 需要解释node.js npm(express)吗?,javascript,node.js,express,Javascript,Node.js,Express,请给我解释一下这条线是怎么工作的 app.get("/speak/:animal", function(req, res) { var sounds = { pig: "OINK", cow: "MOO", dog: "WOOF WOOF", cat: "I hate humans", goldfish: "..." }; var animal = req.params.animal.toL
app.get("/speak/:animal", function(req, res) {
var sounds = {
pig: "OINK",
cow: "MOO",
dog: "WOOF WOOF",
cat: "I hate humans",
goldfish: "..."
};
var animal = req.params.animal.toLowerCase();
var sound = sounds[animal];
res.send("The " + animal + " says '" + sound + "'");
});
还有这条线
app.get("/repeat/:message/:times", function(req, res) {
var message = req.params.message;
var times = Number(req.params.times);
var result = "";
for(var i = 0; i < times; i++) {
result += message + " ";
}
res.send(result);
});
app.get(“/repeat/:message/:times”),函数(req,res){
var message=req.params.message;
var时间=数量(要求参数时间);
var结果=”;
对于(变量i=0;i<次;i++){
结果+=消息+“”;
}
res.send(结果);
});
第一个:
// Listens to all the get requests on the url path /speak/anything
// :animal is a placeholder, anything you pass in the url will be be available in the url param animal.
app.get("/speak/:animal", function(req, res){
// JSON object with a key value pair.
var sounds = {
pig: "OINK",
cow: "MOO",
dog: "WOOF WOOF",
cat: "I hate humans",
goldfish: "..."
};
// Converts the request parameter 'anything' in the url to a lower case string.
var animal = req.params.animal.toLowerCase();
// Tries to find the sound in the sounds object since 'anything' is no key it wont find a suitable value for the sound.
var sound = sounds[animal];
// Responds to the request with the resolved sound.
res.send("The " + animal + " says '" + sound + "'");
第二个
// Let us consider that the request /repeat/received/10
app.get("/repeat/:message/:times", function(req, res){
// the /received in the url will be available to access under the req.param.messages
var message = req.params.message;
// message = received
// Converting the times string 10 passed in the url to a number
var times = Number(req.params.times);
var result = "";
// loop will run from 0 to 9 and keep appending to the string result
for(var i = 0; i < times; i++){
result += message + " ";
}
// At the end of the for loop
// result will contain = 'received received received' 7 more time ...
// will send the response to the request.
res.send(result);
});
<代码> /让我们考虑请求/重复/接收/ 10
app.get(“/repeat/:message/:times”),函数(req,res){
//url中接收的/将可在req.param.messages下访问
var message=req.params.message;
//消息=已收到
//将url中传递的时间字符串10转换为数字
var时间=数量(要求参数时间);
var结果=”;
//循环将从0运行到9,并继续追加到字符串结果
对于(变量i=0;i<次;i++){
结果+=消息+“”;
}
//在for循环的末尾
//结果将包含='received'7更多时间。。。
//将发送对请求的响应。
res.send(结果);
});
1。“汪汪”2。是express.js:哪一行?您在理解什么方面有困难?var animal=req.params.animal///这意味着,在发出get请求时,会在url中为动物传递一个动态值,例如/speak/cat,以了解在url中访问的是哪种资源,您可以使用req.param.animal访问它,在本例中返回cat。