0

I am trying to write a node.js app to fetch movie listing using omdb api. When I try to request the /result route I get the error below:

Error: Can't set headers after they are sent.
    at validateHeader (_http_outgoing.js:494:11)
    at ServerResponse.setHeader (_http_outgoing.js:501:3)
    at ServerResponse.header (/home/cabox/workspace/APIs/movie_search_app/node_modules/express/lib/response.js:767:10)
    at ServerResponse.contentType (/home/cabox/workspace/APIs/movie_search_app/node_modules/express/lib/response.js:595:15)
    at ServerResponse.send (/home/cabox/workspace/APIs/movie_search_app/node_modules/express/lib/response.js:145:14)
    at Request._callback (/home/cabox/workspace/APIs/movie_search_app/app.js:18:13)
    at Request.self.callback (/home/cabox/workspace/APIs/movie_search_app/node_modules/request/request.js:186:22)
    at emitTwo (events.js:126:13)
    at Request.emit (events.js:214:7)
    at Request.<anonymous> (/home/cabox/workspace/APIs/movie_search_app/node_modules/request/request.js:1163:10)

The full code in my app.js file is below:

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

var request = require("request");

app.set("view engine", "ejs")

app.get("/", function(req, res){
    res.send("Hello, it workds! ... and this is the home page");
});


app.get("/results", function(req, res){
    res.send("Hello, it workds!");

    request("http://omdbapi.com/?s=california&apikey=thewdb", function(error, response, body){     
      if (!error && response.statusCode ==200) {      
        var data = JSON.parse(body);
        res.send(results.search(0).title);
        res.render("results", {data: data});
      }  
    });

});


app.listen(3000,function(){
  console.log("Movie App has started!!!");
});

Sincerely hope someone can help provide some guidance on how to handle/resolve this error.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
SDonA
  • 19
  • 2
  • Possible duplicate of [Error: Can't set headers after they are sent to the client](https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client) – Mika Sundland Jan 20 '18 at 12:06

2 Answers2

2

You send already a string as response and try later to send the rendered page to the client, too, which isn't working...

app.get("/results", function(req, res){
  [...]     
  if (!error && response.statusCode ==200) {      
    [...]
    // HERE IS YOUR PROBLEM
    res.send(results.search(0).title);
    res.render("results", {data: data});
  }  
});

What does "res.render" do, and what does the html file look like?

http://expressjs.com/en/api.html

gkhaos
  • 694
  • 9
  • 20
0

@moneydhaze thanks for your response. You made me look through the code thoroughly and start up simply. it appears the res.send and res.render were conflicting. when I took out the res.render and simplified the code; it worked. See the simplified code below:

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

var request = require("request");

app.set("view engine", "ejs")


app.get("/", function(req, res){
  res.send("This is the Home Page")
});


app.get("/results", function(req, res){
    request("http://www.omdbapi.com/?s=guardians+of+the+galaxy&apikey=thewdb", function(error, response, body){
      if (!error && response.statusCode ==200){
        res.send(body);
      }  
    })
});


app.listen(3000,function(){
  console.log("Movie App has started!!!");
});
SDonA
  • 19
  • 2