-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
58 lines (48 loc) · 1.61 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
'use strict';
const http = require('http');
const express = require('./lib/express');
const util = require('./lib/util');
const path = require('path');
const connection = require('./mysql/');
const loginRouter = require('./routers/loginController');
const errorRouter = require('./routers/errorController');
const homeRouter = require('./routers/homeController');
connection.connect(function(err) {
if(err) {
console.log('!!!somethings runing wrong:' + err);
return;
}
console.log('[connection connect] succeed!');
});
global.connection = connection;
const app = new express();
const server = http.createServer((req, res) => {
util.parseUrl(req, res);
util.parseCookie(req, res);
if(req.pathName.indexOf('/static/src') >= 0) {
app.serverStatic(path.join(__dirname, '/static/src'), req, res);
}
// api
app.use('/form/login', loginRouter.handleLogin);
app.use('/form/addUser', loginRouter.handleRegister);
app.use('/upload', homeRouter.handleUpload);
app.use('/get_userinfo', homeRouter.getUserInfo);
app.use('/upload/video', homeRouter.handleUploadVideo);
// renderFile
app.use('/', loginRouter.render('login'));
app.use('/form', loginRouter.render('login'));
app.use('/form/register', loginRouter.render('register'));
app.use('/form/login/error', errorRouter.render({
status: '403',
errMsg: 'username or password error',
}));
app.use('/form/register/error', errorRouter.render({
status: '403',
errMsg: 'user had existed...'
}));
app.use('/home', homeRouter.render);
// handle request
app.handle(req, res);
}).listen(4567, () => {
console.log('server is running at port 4567...');
});