-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
119 lines (99 loc) · 3.38 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
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
'use strict';
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./example/routes');
var navigation = require('./example/routes/navigation.js');
var files = require('./example/routes/files.js')();
var http = require('http');
var path = require('path');
var lessMiddleware = require('less-middleware');
var session = require('./example/routes/session')();
var app = express();
var server = require('http').createServer(app)
var io = require('socket.io').listen(server);
var sender = function(status, result, res) {
var json = JSON.stringify(result);
res.set('Content-Type', 'application/json');
res.send(status, json);
};
// all environments
app.set('port', process.env.PORT || 3010);
app.set('views', path.join(__dirname, 'example', 'views'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
secret: 'secret',
cookie: {expires: false}
}));
app.use(lessMiddleware({
src: __dirname + '/example/public',
compress: true
}));
app.use(express.static(path.join(__dirname, 'example', 'public')));
app.use(express.static(path.join(__dirname, 'bower_components')));
app.use(express.static(path.join(__dirname, 'modules')));
app.use(express.static(path.join(__dirname, '.dist', 'docs')));
app.use(app.router);
app.use(express.errorHandler());
app.post('/api/echo', function(req, res) {
if(req.body.error) {
var error = {
name: 'EchoTestError',
resource: 'api/echo',
statusCode: 401,
message: 'Fake echo test error'
};
sender(error.statusCode, error, res);
}
else {
sender(200, req.body, res);
}
});
app.post('/api/session/setActivity', session.setActivity);
app.post('/api/session/getLastActivity', session.getLastActivity);
app.post('/api/session/getData', session.getData);
app.post('/api/session/setData', session.setData);
app.post('/api/session/deleteData', session.deleteData);
app.post('/api/files/getMarkdown', files.getMarkdown);
app.use('/api/navigation/getTopList', navigation.getTopList);
app.use('/api/navigation/getSubList', navigation.getSubList);
app.get('/doc', function(req, res) {
//res.sendfile(path.join(__dirname, '.dist/docs/index.html'));
res.render('doc/index');
});
app.get('/*', function(req, res) {
//res.sendfile(path.join(__dirname, 'example/views/index.html'));
res.render('index');
});
io.sockets.on('connection', function (socket) {
socket.emit('news', 'push this news from server when client connection is successfully');
socket.on('api/echo', function (data, callback) {
if(data.error) {
var error = {
name: 'EchoTestError',
resource: 'api/echo',
statusCode: 401,
message: 'Fake echo test error'
};
callback(error);
} else{
callback(null, data);
}
});
socket.on('echo', function (data, callback) {
console.log('## Socket: received from the client:');
console.log(data);
callback(null, data);
});
});
server.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});