Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
72 lines (62 loc) · 2.13 KB
/
Copy pathapp.js
File metadata and controls
72 lines (62 loc) · 2.13 KB
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
/*
* @Date: 2020-12-20 21:41:22
* @LastEditors: kanoyami
* @LastEditTime: 2020-12-20 22:03:46
*/
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const history = require('connect-history-api-fallback');
const index = require("./routes/index")
const expressWs = require('express-ws');
const fileUpload = require('express-fileupload')
const opn = require("./utils/auto_open")
const messageHandler = require("./handler/message");
const init = require("./init")
const __CONF__ = require("./config/config.json");
const __PORT__ = __CONF__["serverPort"];
const __UPLOAD_FIFES__ = path.join(process.cwd(),"upload")
//启动配置
init.startup()
const app = express();
app.all('*', (req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "*");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
next();
});
app.use(express.json());
app.use( fileUpload() );
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use("/", index);
app.use("/helper",express.static(path.join(__dirname, 'help')));
app.use(history());
expressWs(app)
app.ws('/chat', function (ws, req) {
ws.setMaxListeners(255)
ws.on('message', messageHandler.bind(ws))
})
app.use("/upload",express.static(__UPLOAD_FIFES__));
app.use(express.static(path.join(__dirname, 'public')));
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// 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
console.log(err)
res.status(err.status || 500);
res.json('error');
});
app.listen(__PORT__);
console.log(`App start on ${__PORT__} of version ${__CONF__["version"]}` )
console.log("get high performance!")
console.log(`打开浏览器,进入http://localhost:${__PORT__}/`)
console.log("今天令荷在阳台。")
opn(`http://localhost:${__PORT__}/`)