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.