-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
101 lines (83 loc) · 3.1 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
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
const express = require('express');
const app = express();
const path = require("path");
const morgan = require('morgan');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const OAuth2Server = require('oauth2-server');
const Request = OAuth2Server.Request
const Response = OAuth2Server.Response;
const movieRoutes = require('./api/routes/movies');
const userInfoRoutes = require('./api/routes/users/info');
const userHistoryRoutes = require('./api/routes/users/history');
const userGenreHistoryRoutes = require('./api/routes/users/genre_history');
const userRecommendRoutes = require('./api/routes/users/recommend');
const userRegisterRoutes = require('./api/routes/users/register');
const userRatingRoutes = require('./api/routes/users/rating');
app.oauth = new OAuth2Server({
model: require('./oauthModel.js'),
accessTokenLifetime: 60 * 60 * 24 * 365,
allowBearerTokensInQueryString: true
});
global.rootDir = path.resolve(__dirname);;
mongoose.connect('mongodb+srv://admin:[email protected]/test?retryWrites=true&w=majority'
, { useNewUrlParser: true });
// app.use(redirectToHTTPS([/localhost:(\d{4})/], [/\/insecure/], 301));
app.use(morgan('dev'));
app.use('/file', express.static('file'));
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// for broswer CORS error
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization, application/x-www-form-urlencoded');
if(req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET');
return res.status(200).json({});
}
next();
});
// oAuth token
app.all('/users/login', obtainToken);
app.use('/users/register', userRegisterRoutes);
app.use('/users/info', authenticateRequest, userInfoRoutes);
app.use('/users/history', authenticateRequest, userHistoryRoutes);
app.use('/users/genre_history', authenticateRequest, userGenreHistoryRoutes);
app.use('/users/rating', authenticateRequest, userRatingRoutes);
app.use('/users/recommend', authenticateRequest, userRecommendRoutes);
app.use('/movies', movieRoutes);
app.use((req, res, next) => {
const error = new Error('Not found');
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
error:{
message: error.message
}
});
});
function obtainToken(req, res) {
var request = new Request(req);
var response = new Response(res);
return app.oauth.token(request, response)
.then(function (token) {
res.json(token);
}).catch(function (err) {
res.status(err.code || 500).json(err);
});
}
function authenticateRequest(req, res, next) {
var request = new Request(req);
var response = new Response(res);
return app.oauth.authenticate(request, response)
.then(function (token) {
next();
}).catch(function (err) {
res.status(err.code || 500).json(err);
});
}
module.exports = app;