-
Notifications
You must be signed in to change notification settings - Fork 237
/
app.js
78 lines (64 loc) · 2.12 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
'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var lactate = require('lactate');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var pwd = __dirname;
var app = express();
var router = express.Router();
var port = 3030;
var routers = require('./RESTful_API/apis/index.js');
var dbConfig = require('./RESTful_API/db/config.js');
var env = require('./RESTful_API/env.config.js');
global.userPath = './User';
global.dbHandel = require('./RESTful_API/db/dbHandel.js');
global.db = mongoose.connect('mongodb://' + dbConfig[env].host + ':' + dbConfig[env].port + '/' + dbConfig[env].database, {
user: dbConfig[env].username,
pass: dbConfig[env].password
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(session({
secret: 'who am i ?',
cookie: { maxAge: 1000 * 60 * 60 * 24 * 7 },
saveUninitialized: true,
resave: true
}));
// 用户上传的图片
app.use('/img', express.static(pwd + '/User/UploadImg/'));
// 用户上传的音乐
app.use('/audio', express.static(pwd + '/User/UploadMusic/'));
// 后台静态资源
app.use('/back', express.static(pwd + '/Back_Stage/'));
// 前台静态资源
app.use('/front', express.static(pwd + '/Front_Stage/'));
// 后台页面
app.get('/', function(req, res, next) {
if (!req.session.isLogin) {
res.clearCookie('isLogin');
res.clearCookie('user_name');
}
res.sendFile(pwd + '/Back_Stage/index.html',{});
})
app.engine('.html', require('ejs').__express);
app.set('view engine', 'html');
// 前台页面
app.get('/show/:id', function(req, res, next) {
var id = req.params.id;
var Work = global.dbHandel.getModel('work');
Work.find({ '_id': id }).exec(function(err, docs) {
res.render(pwd + '/Front_Stage/index.html', {
workData: docs[0]
});
})
})
routers.forEach(function(Router) {
app.use('/api', Router);
})
app.listen(port);
console.log('server listening on port: ' + port);
module.exports = app;