-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
110 lines (99 loc) · 2.51 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
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const mongoose = require('mongoose');
const url = require('./config/db.js').url;
mongoose.connect(url);
const session = require('express-session');
const passport = require('passport');
const flash = require('connect-flash');
const books = require('google-books-search');
const app = express();
// Set Up Template Engine
app.set('view engine','pug');
app.set('views','./views');
app.use(express.static('public'));
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
let db = mongoose.connection;
db.on('error', function(err) {
console.log(err);
});
db.on('open', function() {
console.log('Connected to DB');
});
const Book = require('./models').Book;
const User = require('./models').User;
const Trade = require('./models').Trade;
// Passport Config
require('./config/passport.js')(passport);
// Book API
const option = require('./config/bookapi.js');
// Routes
const auth = require('./routes/auth.js');
app.use('/auth', auth);
const profile = require('./routes/profile.js');
app.use('/profile',profile);
const trade = require('./routes/trade.js');
app.use('/trade',trade);
const 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('index', {
title: 'Home',
books: results
});
}
});
});
app.get('/search', function(req,res) {
let title = req.query.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!');
}
});
});
// Start Server
app.listen(process.env.PORT || 3000, function() {
console.log(`Listening on port ${process.env.PORT}`);
});