-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.js
76 lines (54 loc) · 1.6 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//Setting up the express Server
const express = require('express')
const path = require('path');
const hbs = require('hbs');
const movieSearchAPICall = require('./utils/movieSearch');
const port =3000
const app = express()
// Serving static files
const publicDirectoryPath = path.join(__dirname,'../public');
app.use(express.static(publicDirectoryPath));
// Setting up hbs template engine for serving dynamic pages
app.set('view engine','hbs');
const viewsPath = path.join(__dirname,'../templates/views');
const partialsPath = path.join(__dirname,'../templates/partials');
app.set('views',viewsPath);
hbs.registerPartials(partialsPath)
app.get('/',(req,res)=>{
res.render('index');
})
app.get('/movie',(req,res)=>{
if(!req.query.name){
return res.send({
error:"Your query string is empty"
})
}
movieSearchAPICall(req.query.name,(error,movieInfo)=>{
if(error){
return console.log(err);
}
else if(movieInfo.Response == 'False'){
return console.log( `We were unable to find ${req.query.name}`);
}
return res.send({
movieName:movieInfo.Title,
YearofRelease :movieInfo.Year,
rating:movieInfo.imdbRating,
imgSrc :movieInfo.Poster,
director:movieInfo.Director,
})
})
})
app.get('/guess',(req,res)=>{
if(!req.query.id){
return res.send({
error:"No id was passed"
})
}})
// To handle error pages
app.get('*',(req,res)=>{
res.render('error')
})
app.listen(port,()=>{
console.log('Server is up on 3000')
})