title: NodeJS response.render():Can't set headers after they are sent
body: <p>So, the first render is successful <code>app.get("/", function(request, response)</code>, but when I process <code>app.post("/search", urlencodedParser, function(request, response)</code></p>
  
  <p>I get the Error: </p>
  
  <blockquote>
    <p>c:\weather\views\main.hbs: Can't set headers after they are sent.</p>
  </blockquote>
  
  <p>I read that, <code>The function</code>res.render()<code>should only be called once per request.</code>, but I don't understand - How Can I change datas using <code>views(Handlebars)</code>, if i can't call render again with new datas?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var express = require("express");
  var fs = require("fs");
  var bodyParser = require("body-parser");
  var hbs = require("hbs");
  // create object
  var app = express();
  //urlEncoded
  var urlencodedParser = bodyParser.urlencoded({
    extended: false
  });
  //read city.json
  var content = fs.readFileSync("./city.list.json", "utf8");
  var cities = JSON.parse(content);
  //set view Handlebars
  app.set("view engine", "hbs");
  //register Helper - ResultBlock
  hbs.registerHelper("ResultBlock", function(array) {
    var res = "";
    if (array[0] == "false") {
      res = '&lt;div class="alert alert-warning alert-dismissible fade show"&gt;&lt;button type="button" class="close" data-dismiss="alert"&gt;&amp;times;&lt;/button&gt;&lt;strong&gt;Not Found!&lt;/strong&gt; The ' + array[1] + ' is not correct.&lt;/div&gt;';
    } else if (array[0] == "true") {
      res = 'Succsesful!!!';
    }
    return new hbs.SafeString(res);
  })
  app.get("/", function(request, response) {
    //response.sendfile('index.html');
    response.render("main.hbs", {
      city_value: "Zaporozhye",
      data: []
    })
  });
  app.post("/search", urlencodedParser, function(request, response) {
    var city = request.body.city;
    var idCity = -1;
    for (let i = 0; i &lt; cities.length; i++) {
      if (cities[i].name == city) {
        idCity = cities[i].id;
        break;
      }
    }
  
    if (idCity != -1) {
      response.render("main.hbs", {
        city_value: city,
        data: ["true", city]
      })
    } else {
      response.render("main.hbs", {
        city_value: city,
        data: ["false", city]
      })
    }
    response.end();
  });
  
  app.listen(3000);</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  
  &lt;head&gt;
    &lt;title&gt;Document&lt;/title&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;meta http-equiv="X-UA-Compatible" content="ie=edge"&gt;
    &lt;script src="app.js" type=""&gt;&lt;/script&gt;
    &lt;link rel="stylesheet" href="style.css"&gt;
    &lt;!-- Latest compiled and minified CSS --&gt;
    &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"&gt;
  
    &lt;!-- jQuery library --&gt;
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  
    &lt;!-- Popper JS --&gt;
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"&gt;&lt;/script&gt;
  
    &lt;!-- Latest compiled JavaScript --&gt;
    &lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
    &lt;div class="container-fluid" style="background-color:black; color:darkorange;"&gt;
      &lt;div class="row"&gt;
        &lt;div class="col"&gt;
          &lt;h1&gt;Weather&lt;/h1&gt;
        &lt;/div&gt;
        &lt;div class="col-*-*"&gt;
          &lt;form action="/search" method="post"&gt;
            &lt;div class="row"&gt;
              &lt;div class="col"&gt;
                &lt;input type="text" class="form-control" name="city" value="{{city_value}}" /&gt;
              &lt;/div&gt;
              &lt;div class="col"&gt;
                &lt;button type="submit" class="btn btn-primary"&gt;Search&lt;/button&gt;
              &lt;/div&gt;
            &lt;/div&gt;
          &lt;/form&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div id="result_block" class="container"&gt;
      {{ResultBlock data}}
    &lt;/div&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>