简体   繁体   中英

Can I use res.send() node.js method inside the loop?

I am beginner in node.js. I have created node server and write simple program using express, sending response using res.send() method. If a user visit the route like "/repeat/hello/5" should print hello 5 times and there are spaces between the word. Example : hello hello hello hello hello

var express = require("express");
var app = express();

var animalsData = {
    "dog" : "Bow Bow",
    "cat" : "Meow",
    "pig" : "Oink",
    "horse" : "Hiha",
    "lion" : "Roar"
}

app.get("/", function(req,res){
    res.send("Hi there! Welcome to my asssingment.");
});

app.get("/speak/:animal", function(req, res){
    var name = req.params.animal;

    if(animalsData[name]){
        res.send("The " +name+ " says '" +animalsData[name]+ "'.");
    } else
        res.send("I don't know what " +name+ " say.");
});

app.get("/repeat/:word/:times", function(req, res){
    var word = req.params.word;
    var times = parseInt(req.params.times);

    for(var i = 1; i<= times; i++){
        res.send(word);   
     }
});

app.listen(process.env.PORT, process.env.IP);

as first answer you can do something like.

app.get("/repeat/:word/:times", function(req, res){
    var word = req.params.word;
    var times = parseInt(req.params.times);
    for(var i = 1; i<= times; i++){
         word = ' ' + word;  
     }
   res.send(word);
});

if you want a streaming api the look this documentation server-send-events(sse)

No.

Look at the documentation :

Sends the HTTP response

You can't have multiple responses to a single request.


Build a single response body in a variable as you loop. Send it once the loop is finished.

app.get("/repeat/:word/:times", function(req, res) {
    var word = req.params.word;
    var times = parseInt(req.params.times);
    var x = '';
    var y = word + ' ';
    for (var i = 1; i <= times; i++) {
        x += y;
    }
    res.send(x);
});

I tried this and it worked.

It worked by wrapping the for loop in a function, and then

res.send(functionName(times));

Since I am also a beginner, I don´t think it is the best practice, but it worked...

app.get("/repeat/:word/:times", function(req, res){
    var word = req.params.word;
    var times = parseInt(req.params.times);
    var str = ''

    function myFunction(n){
    for(var i = 1; i <= times; i++){
        str += word + ' ';
    }
        return str;
    }
        res.send(myFunction(times));
    });

instead of using res.send, use res.write then res.end outside the loop, because you can only send responses one time so it doesn't work in a loop..

app.get("/repeat/:word/:num", function(req, res){
var number = req.params.num;
var word = req.params.word;
var numnum = parseInt(number);
for(var i = 0; i < numnum ; i++ ){
res.write(" " + word + " ");
}
res.end();
});

Sending response again on the same request will give you Can't set headers error.

One request can have just one response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM