-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
54 lines (42 loc) · 1.36 KB
/
server.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// App Starting Point : server.js
// Required Packages for Fortena
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
//Initilization of Express Middleware
const app = express()
// OMDB API key
const apiKey = process.env.APIKEY;
//Static Folder for Styles
app.use(express.static('public'));
//BodyParser Middleware
app.use(bodyParser.urlencoded({ extended: true }));
//View Engine for rendering pages
app.set('view engine', 'ejs')
// Route for homepage
app.get('/', function (req, res) {
res.render('index', {Movie: null, error: null});
})
app.post('/', function (req, res) {
let movieName = req.body.movieInput;
let url = "http://www.omdbapi.com/?i=tt3896198&apikey="+apiKey+"&t=" + movieName + "&y=&plot=full&tomatoes=true&r=json"
request(url, function (err, response, body) {
if(err){
res.render('index', {Movie: null, error: 'Error, please try again'});
} else {
let MovieDetails = JSON.parse(body)
if(MovieDetails.Response==false){
res.render('index', {Movie: null, error: 'Error, please try again'});
} else {
res.render('index', {Movie: MovieDetails, error: null});
}
}
});
})
// Set Port
const port = process.env.PORT || 3000;
// Start server
app.listen(port, () => {
console.log(`Server started on ${port}`);
})
module.exports = app;