server.on -> loop

What am I doing wrong.. Seems that I can't put a loop in server.on( { }

How am I supposed to do it, if I wanna do it the right way?

      server.on("/blink", [](){
      // the loop function runs over and over again forever
          void loop() {
            digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
            delay(1000);                       // wait for a second
            digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
            delay(1000);                       // wait for a second
          }
  });

Seems that I can't put a loop in server.on

Sure you can. You should NOT be defining loop() in the body of the lambda expression, though. You can use ANY kind of looping construct, though.

Did you try this?

server.on("/blink", []() {
  // the loop function runs over and over again forever
  loop();
});

 void loop() {
    digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);                       // wait for a second
    digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);                       // wait for a second
  }

johnwasser:
Did you try this?

server.on("/blink", []() {

// the loop function runs over and over again forever
 loop();
});

void loop() {
   digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
   delay(1000);                       // wait for a second
   digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
   delay(1000);                       // wait for a second
 }

use an anonymous function to call unique function... why?

the problem in the code is (as Paul pointed out) is the issue with declaring a (very poorly named) function within the lambda. a looping construct can certainly be put into the lambda.

the OP may not literally want "the loop function runs over and over again forever" it looks like a cut and paste issue to me.

So you think they meant one of these?

server.on("/blink", []() {
  // the loop function runs over and over again forever
  digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
});
server.on("/blink", []() {
  // the loop function runs over and over again forever
  while (true) {
    digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);                       // wait for a second
    digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);     // wait for a second
  }
});

In that last example, the while loop will never end, and the server will ever get a chance to process another client request.

OP: I think you simply need to set a flag in the lambda expression, and deal with toggling the LED in loop(), if the flag is set.