-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
38 lines (28 loc) · 844 Bytes
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
app.set("view engine", "ejs");
app.use(express.static("public"));
var request = require('request');
app.get("/", function(req,res){
res.render("search");
});
app.get("/request", function(req, res){
var query = encodeURIComponent(req.query.search);
var url = "http://www.omdbapi.com/?s=" + query + "&apikey=****";
request(url, function(error, response, body){
var data = JSON.parse(body);
if(!data["Error"] && response.statusCode === 200){
res.render("results", {data: data});
}
else{
res.redirect("error");
}
})
});
app.get("*", function(req, res){
res.render("error");
});
app.listen(port, function(){
console.log("Server running");
});