-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
136 lines (125 loc) · 3.18 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var compression = require('compression');
var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var helmet = require('helmet');
var session = require('express-session');
var morgan = require('morgan');
var mongoose = require('mongoose');
var url = require('./config/db.js').url;
mongoose.connect(url);
var passport = require('passport');
var flash = require('connect-flash');
var favicon = require('serve-favicon')
var path = require('path');
var port = process.env.PORT || 3000;
var books = require('google-books-search');
var app = express();
/* ****************************************** */
app.use(compression());
app.set('view engine', 'ejs');
app.set('views', './views');
app.use(express.static('public'));
app.use(helmet());
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json());
app.use(cookieParser());
app.use(session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(function(req, res, next) {
res.locals.user = req.user;
next();
});
/* ****************************************** */
//DB
var db = mongoose.connection;
db.on('error', function(error){
console.log(error);
});
db.on('open', function(){
console.log('Connected to DB');
});
var User = require('./models/user.js');
var Book = require('./models/book.js');
var Trade = require('./models/trade.js');
/* ****************************************** */
//Passport Config
require('./config/passport.js')(passport);
/* ****************************************** */
//BOOK API
var option = require('./config/bookAPI.js');
// Route
var auth = require('./routes/auth.js');
app.use('/auth', auth);
var profile = require('./routes/profile.js');
app.use('/profile', profile);
var trade = require('./routes/trade.js');
app.use('/trade', trade);
var allbooks = require('./routes/allbooks.js');
app.use('/allbooks', allbooks);
// App start
app.get('/', function(req, res){
books.search('Web Development', {
field: "title",
offset: 0,
limit: 9,
type: 'books',
order: 'relevance',
lang: 'en'
}, function(error, results, apiResponse){
if(error){
res.send(error);
} else {
res.render('home', {
title: "Home",
books: results
})
}
})
});
//Testing Book API
// app.get('/api', function(req, res){
// books.search('Monkey', option, function(error, results, apiResponse){
// if(!error){
// res.send(results);
// } else {
// console.log(error);
// }
// })
// });
app.get('/search', function(req, res){
var title = req.query.title;
//console.log(title);
books.search(title, {
field: "title",
offset: 0,
limit: 40,
type: 'books',
order: 'relevance',
lang: 'en'
}, function(error, results, apiResponse){
if(!error){
res.render('search', {
title: 'Search Book',
books: results
})
} else {
console.log(error);
res.status(404).send('File Not Found!');
}
})
});
app.listen(port, function(err){
if(err){
console.log(err);
return;
}
console.log('listening on ' + port);
})