Ashutosh
Ashutosh

Reputation: 1095

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);

Upvotes: 4

Views: 9741

Answers (6)

SukiCodes
SukiCodes

Reputation: 21

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();
});

Upvotes: 1

jianyang hutdaugg
jianyang hutdaugg

Reputation: 11

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.

Upvotes: 1

Bruno Aguiar
Bruno Aguiar

Reputation: 1

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));
    });

Upvotes: 0

Rahul
Rahul

Reputation: 952

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

One request can have just one response.

Upvotes: -3

zabusa
zabusa

Reputation: 2719

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)

Upvotes: 4

Quentin
Quentin

Reputation: 944054

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.

Upvotes: 2

Related Questions