Express: Can't set headers after they are sent
<p>Following is my server file. I am making 2 calls, one post and one get. It works fine at times. But gives an error of : Can't set headers after they are sent. Does this have anything to do with my client side code?</p>

<p><strong>server.js</strong></p>

<pre><code>var express    = require('express')
var mongoose   = require('mongoose')
var path       = require('path')
var bodyParser = require("body-parser")
var cors       = require("cors")
var app        = express()
var port       = process.env.PORT || 3000
var Url        = require("./data/url-schema");


//Express request pipeline
app.use(express.static(path.join(__dirname,"../client")))
app.use(bodyParser.json())
app.use(cors());

/*
Your server must be ready to handle real URLs. When the app first loads at / it will probably work, but as the user navigates around and then hits refresh at /dashboard your web server will get a request to /dashboard. You will need it to handle that URL and include your JavaScript application in the response.
 */
app.get('*', function (request, response, next){
  response.sendFile(path.resolve(__dirname, '../client', 'index.html'))
  next()
})

app.get('/:code', function(req, res) {
    console.log("reg", req.params.code)
    Url.findOne({code:req.params.code}, function(err, data){
    console.log("data", data)
    if(data)
            res.redirect(302, data.longUrl)
        else
            res.end()
    })
})

app.post('/addUrl', function (req, res, next) {
    console.log("on create");
    Url.findOne({longUrl:req.body.longUrl}, function(err, data) {
    if (err)
      res.send(err);
    else if(data) {
        console.log("already exists",data)
      res.send("http://localhost:3000/"+data.code);
    } else {
            var url = new Url({
                code    : Utility.randomString(6,"abcdefghijklm"),
                longUrl : req.body.longUrl
              });
            console.log("in last else data created",url)
              url.save(function (err, data) {
                console.log(data)
                if (err)
                  res.send(err);
                else
                  res.send("http://localhost:3000/"+data.code);
              });
            }
    });
})

app.listen(port, function () {
  console.log('Example app listening on port 3000!')
});

// Connect to our mongo database
mongoose.connect('mongodb://localhost/shortUrl');
</code></pre>

<p>I get the Following error</p>

<p><strong>error</strong></p>

<pre><code>_http_outgoing.js:335
    throw new Error('Can\'t set headers after they are sent.');
          ^
Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
    at ServerResponse.header (/opt/lampp/htdocs/url-shortener/node_modules/express/lib/response.js:718:10)
    at ServerResponse.location (/opt/lampp/htdocs/url-shortener/node_modules/express/lib/response.js:835:8)
    at ServerResponse.redirect (/opt/lampp/htdocs/url-shortener/node_modules/express/lib/response.js:874:8)
    at Query.&lt;anonymous&gt; (/opt/lampp/htdocs/url-shortener/server/server.js:30:8)
    at /opt/lampp/htdocs/url-shortener/node_modules/mongoose/node_modules/kareem/index.js:177:19
    at /opt/lampp/htdocs/url-shortener/node_modules/mongoose/node_modules/kareem/index.js:109:16
    at process._tickCallback (node.js:355:11)
</code></pre>