-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
137 lines (124 loc) · 3.92 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
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
137
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var log = require('./utils/logger');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var util = require('./utils/response');
var message = require('./utils/messages.json');
//Import the mongoose module
var mongoose = require('mongoose');
var indexRouter = require('./routes/index');
var apiRouter = require('./routes/api');
var app = express();
var User = require('./models/user')
//Set up default mongoose connection
var mongoDB = 'mongodb://127.0.0.1/new_mern_database';
mongoose.connect(mongoDB);
// Get Mongoose to use the global promise library
mongoose.Promise = global.Promise;
//Get the default connection
var db = mongoose.connection;
//Bind connection to error event (to get notification of connection errors)
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
db.once('open', function() {
console.log("connected!")
/* var test = new Photographer({
first_name : 'test',
family_name:'test',
username:'test',
password:'test',
email:'test',
date_of_birth : new Date
});
test.save(function(err, test){
if(err) return console.error
}) */
/*
var fluffy = new Kitten({ name: 'Testing' });
fluffy.save(function (err, fluffy) {
if (err) return console.error(err);
fluffy.speak();
});
*/
/* var shakespeare = new Book({
name : "Macbeth",
author: "Shakespeare",
date: new Date
})
shakespeare.name = "Romeo and Juliet"
// shakespeare.save(function(err){
// if(err) return handleError(err);
// })
Book.find({'name':'Romeo and Juliet'},function (err, books) {
if (err) return console.error(err);
console.log(books);
}) */
});
app.set('trust proxy', 1)
app.use(
session({
secret: 'mern',
resave: false,
store: new MongoStore(
{mongooseConnection:mongoose.connection}
)
})
);
//morgan middleware
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser('mern'));
app.use(function(req, res, next) {
console.log(req.url);
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
next();
});
app.use(express.static(path.join(__dirname, 'client/build')));
app.all('*', checkSession);
function checkSession(req, res, next) {
console.log("checkSession req.session.user_id: "+req.session.session_user_id);
if(req.session.session_user_id!=null || req.url=="/api/login" || req.url=="/api/user/create" /* || req.url=="/api/forget_password" || req.url=="/api/check_reset_pass_para" || req.url=="/api/re_update_password" */){
console.log("without login");
if(req.url=="/api/logout"){
console.log("logout call");
req.session.destroy(function(err) {
if(err)next()
res.send(util.success({}, message.logout));
})
}else{
console.log(res.body)
console.log("return response");
next();
}
}
else
{
console.log(res.body)
res.send(util.error({}, message.common_messages_error));
/* res.status(403).send('Something broke!') */
}
}
//express static middleware
/* app.use(express.static(path.join(__dirname, 'public'))); */
app.use('/', indexRouter);
app.use('/api', apiRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404,'Second'));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.send(util.error({}, message.common_messages_error));
});
module.exports = app;