This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
53 lines (42 loc) · 1.65 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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var morgan = require('morgan');
var mongoose = require('mongoose');
var passport = require('passport');
var jwt = require('jwt-simple');
var authRouter = require('./server/routes/auth-router');
var booksRouter = require('./server/routes/books-router');
var loansRouter = require('./server/routes/loans-router');
var indexRouter = require('./server/routes/index-router');
var User = require('./server/models/user');
var config = require('./config.js');
var port = process.env.PORT || 3333;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(morgan('dev'));
app.use(passport.initialize());
// Mongoose confuguration (MongoLabs)
mongoose.connect(config.database, function(err){
if (err) console.log(err);
});
require('./server/auth/passport-strategy')(passport);
// router.get('/api', function(req, res) {
// res.json({message: 'Welcome to the Lightshelf API!'});
// });
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
res.header("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT");
if(req.method === 'OPTIONS') {
res.sendStatus(200);
return;
}
next();
});
app.use('/api', indexRouter);
app.use('/api/auth', authRouter);
app.use('/api/books', passport.authenticate('jwt', { session: false }), booksRouter);
// app.use('/api/loans', passport.authenticate('jwt', { session: false }), loansRouter);
app.listen(port);
require('util').log('Library hosted on port ' + port);