0

I trying to use a loop within Express.js and cannot end it.

My loop always writes "hello" I print after /hello/SomeNumber (for example /hello/5) and show me 5 hello's.

I want it to stop after >3 and display the message under "*".

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


app.get("*", function(req, res) {
  res.send("Sorry, page not found...What are you doing with your life?");
});

app.listen(process.env.PORT, process.env.IP, function() {
  console.log("Server has started!!!");
});
Andrew Lohr
  • 5,380
  • 1
  • 26
  • 38
Artem Mazur
  • 25
  • 1
  • 6
  • 2
    Use the [break](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break) statement. – Adam H Aug 13 '18 at 19:41

4 Answers4

0

use break;

for (var i = 1; i <= numbers; i++) {
  if(i>3)
  break;
   lol +=  word + " ";
}
Atul
  • 420
  • 2
  • 10
0

Man, the guys above dont unterstand the task. You need to jump to the next middleware if the count of iterations more than 3, right ?

Try this:

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

app.get('/repeat/:word/:numbers', function(req, res, next) {
  var word = req.params.word;
  var numbers = Number(req.params.numbers);
  var lol = '';
  for (var i = 1; i <= numbers; i++) {
    if (i > 3) next()
    lol += word + ' ';
  }
  res.send(lol);
});

app.get('*', function(req, res) {
  res.send('Sorry, page not found...What are you doing with your life?');
});

app.listen(process.env.PORT, process.env.IP, function() {
  console.log('Server has started!!!');
});
  • yes you understand my task! But micro missclick i'd mean !=3 =) – Artem Mazur Aug 13 '18 at 19:55
  • @ArtemMazur Is this working fine for you? I saw you chose this as your answer after choosing mine. I am doing the same without any need of extra middleware. Also, this answer will send response multiple times as `next()` is inside a loop, which will give you error `Error: Can't set headers after they are sent.` – Sookie Singh Aug 13 '18 at 20:38
  • @SookieSingh yes i have this error , but next() i think should work like i want , if i write >3 work fine ,but if write like i need !=3 i have this error – Artem Mazur Aug 13 '18 at 21:10
0

Here's how I would solve it. Add an if statement in your loop that checks if the number > 3. If it is, use break to stop the loop and assign the text from the * route to the lol variable. Then at the end of your route function when 'res.send' is called, it either contains n * hallo or "Sorry, page not found...What are you doing with your life?"

Here's the code. I hope this helps.

app.get("/repeat/:word/:numbers", function(req, res) {
  var word = req.params.word;
  var numbers = Number(req.params.numbers);
  var lol = ""
  for (var i = 1; i <= numbers; i++) {
    if (numbers > 3) {
      break;
      lol = 'Sorry, page not found...What are you doing with your life?';
    } else {
      lol += word + " ";
    }
  }
  res.send(lol);
});


app.get("*", function(req, res) {
  res.send("Sorry, page not found...What are you doing with your life?");
});

app.listen(process.env.PORT, process.env.IP, function() {
  console.log("Server has started!!!");
});
Roy Scheffers
  • 3,832
  • 11
  • 31
  • 36
-1

Something like this?

app.get("/repeat/:word/:numbers", function(req, res) {
    var word = req.params.word;
    var numbers = Number(req.params.numbers);
    if (numbers > 3) {
        res.send("Sorry, page not found...What are you doing with your life?");
    } else {
        var lol = ""
        for (var i = 1; i <= numbers; i++) {
            lol += word + " ";
        }
        res.send(lol);
    }

});

Edit: My answer was no longer accepted after OP accepted Enthusiastic Developer's answer, which as far as I know will give error cause, next() is inside a loop, which will give error Error: Can't set headers after they are sent.

If you definitely want to use a middleware to catch all the requests then here:

app.get("/repeat/:word/:numbers", function(req, res, next) {
    var word = req.params.word;
    var numbers = Number(req.params.numbers);
    if (numbers > 3) {
        next();
    } else {
        var lol = ""
        for (var i = 1; i <= numbers; i++) {
            lol += word + " ";
        }
        res.send(lol);
    }
});


app.all("*", function(req, res) {
    res.send("Sorry, page not found...What are you doing with your life?");
});
Sookie Singh
  • 1,543
  • 11
  • 17
  • Thanks it was nice solution and work fine, but i have a question about what next() doing? this work only in node\express? or everywhere in js? – Artem Mazur Aug 13 '18 at 22:37
  • Here you can read about it: https://stackoverflow.com/questions/5384526/javascript-node-js-next – Sookie Singh Aug 14 '18 at 19:33