forked from hangaoke1/blog-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
111 lines (110 loc) · 3.9 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
const index = require('./routes/index.js');
const express = require('express');
const app = express();
const fs = require('fs')
const path = require('path')
const favicon = require('serve-favicon');
const bodyParser = require('body-parser')
const multiparty = require('multiparty');
const cookieParser = require('cookie-parser')
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
const morgan = require('morgan');
// 启动NODE_ENV=production node app.js
// console.log(process.env.NODE_ENV)
const http = require('http');
const https = require('https');
var certificate = fs.readFileSync('file.crt', 'utf8');
var privateKey = fs.readFileSync('private.pem', 'utf8'),
credentials = { key: privateKey, cert: certificate };
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
// socket
var webSocket = require('./socket/warn.js');
webSocket.listen(httpServer);
// redis
var redis = require('./redis/redis.js').reids;
var sub = require('./redis/redis.js').redis;
var pub = require('./redis/redis.js').pub;
// 设置跨域访问
app.all('*', function(req, res, next) {
// console.log(req.headers.origin);
res.header("Access-Control-Allow-Origin", req.headers.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");
res.header("Access-Control-Allow-Credentials", true); //带cookies
res.header("X-Powered-By", ' 3.2.1')
// res.header("Content-Type", "application/json;charset=utf-8");
if (req.method == 'OPTIONS') {
res.send(200);
} else if (req.method == 'GET') {
req.body = req.query;
next();
} else {
next();
}
});
// app.use(morgan('short'));
app.use(cookieParser('sessiontest'));
app.use(session({
store: new RedisStore({
client: redis,
prefix: 'hgk'
}),
cookie: { maxAge: 1 * 60 * 60 * 1000 }, //默认1小时
secret: 'sessiontest', //与cookieParser中的一致
resave: true,
saveUninitialized: true
}));
//模板引擎
app.set('views', path.join(__dirname + '/views'));
app.engine('.html', require('ejs').__express)
app.set('view engine', 'ejs')
app.use(favicon('./favicon.ico'))
// body 解析中间件
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
// cookie 解析中间件
app.use(cookieParser())
// 设置 express 根目录
app.use(express.static(path.join(__dirname, 'public')))
app.use('/hgk', function(req, res) {
res.end('hello hangaoke');
})
// websocket全网告示快捷入口
io.of('/warnTips').adapter.customHook = function(data, cb) {
io.of('/warnTips').to('room1').emit('warn', JSON.parse(data));
cb('hello ' + data);
}
app.use('/warn', function(req, res, next) {
var time = req.body.time;
var message = req.body.message;
var msg = {
time,
message
}
console.log(io.of('/warnTips').adapter.sids)
// pub.publish('hgk','aaaaaaa1');
// res.end('hello world')
io.of('/warnTips').adapter.customRequest(JSON.stringify(msg), function(err, replies) {
console.log(replies); // an array ['hello john', ...] with one element per node
});
io.of('/warnTips').adapter.clients(function(error, clients) {
if (error) throw error;
console.log(clients); // => [Anw2LatarvGVVXEIAAAD]
res.end('res by app.js hello world! clients in room1:' + clients.length + '-------' + JSON.stringify(user));
});
})
app.use('/', index)
//全局error中间件
app.use(function(err, req, res, next) {
console.log("Error happens", err.stack);
res.status(500);
res.send(err.stack);
});
httpServer.listen(3000, function() {
console.log('HTTP Server is running on: http://localhost:%s', 3000);
});
// httpsServer.listen(3001, function() {
// console.log('HTTPS Server is running on: http://localhost:%s', 3001);
// });