forked from zhanglei923/rk-offlinedev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerRs.js
77 lines (66 loc) · 2.62 KB
/
ServerRs.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
//说明,这是离线开发的server
var express = require('express');
let compression = require('compression')
var app = express();
var fs = require('fs');
var https = require('https');
var http = require('http');
var bodyParser = require('body-parser')
var _ = require('lodash')
var privateKey = fs.readFileSync('./offlinedev/sslKey/v2/private.pem','utf8');
var certificate = fs.readFileSync('./offlinedev/sslKey/v2/file.crt','utf8');
var getConfig = require('./offlinedev/jsmodule/config/configUtil')
var localStatus = require('./offlinedev/jsmodule/config/statusUtil')
var credentials = {key: privateKey, cert: certificate};
let userConfig = getConfig.getUserConfig();
var webPath = getConfig.getWebAppFolder();
if(!fs.existsSync(webPath)){
console.error('致命错误!web工程目录不存在,请检查user-config文件!:', webPath)
console.error('How to fix: mondify "%rk-offlinedev%/user-config.json" to assign your web project path')
return;
}
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
var PORT = 667;
var SSLPORT = 444;
// app.use(bodyParser.urlencoded({ extended: false }))
// app.use(bodyParser.json())
app.use(bodyParser.json({limit: '100mb'}));
app.use(bodyParser.urlencoded({limit: '100mb', extended: true}));
app.use(compression())//gzip
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
//res.header("Access-Control-Allow-Credentials", true);
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("Content-Type", "application/json;charset=utf-8");
next();
});
//全局拦截器
app.use(function (req, res, next) {
const static_proxy = require('./offlinedev/jsmodule/static-proxy/static-proxy');
static_proxy.linkToStaticFile(req, res, next)
});
//静态资源转接到web
app.use('/', express.static(webPath));//注意:必须在全局拦截器之后,否则拦截器无法运行
//action转接
app.get('*', function(req, res, next) {
next()
});
// //启动
module.exports = {
startHttp:()=>{
// var server = httpServer.listen(PORT, function() {
// var host = server.address().address;
// var port = server.address().port;
// console.log('STATIC-RS http://localhost:%s', port);
// });
},
startHttps:(succ)=>{
httpsServer.listen(SSLPORT, function() {
console.log('[STATIC-RS] https://localhost:%s', SSLPORT);
console.log('----------')
succ()
});
}
}