-
Notifications
You must be signed in to change notification settings - Fork 24
/
app.js
84 lines (74 loc) · 2.57 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
const express = require('express');
const bodyParser = require('body-parser')
const {program} = require('commander');
const log = require('./app/util/LogHelper')
const common = require('./app/util/common')
const client = require('./app/util/client')
const commonHandle = require('./app/controller/common')
const encryptHandle = require('./app/controller/encrypt')
const app = express();
log.LogHelper.Init();
// commandline arguments
function parsePort(defaultPort) {
program.version('0.0.1');
program
.version('0.0.1')
.usage('[options] [value ...]')
.option('-p, --port <number>', 'port', common.myParseInt)
program.parse(process.argv);
const options = program.opts();
if (options.port === undefined) {
return defaultPort;
}
return options.port;
}
// CORS & Preflight request
app.use((req, res, next) => {
if (req.path !== '/' && !req.path.includes('.')) {
res.set({
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Origin': req.headers.origin || '*',
'Access-Control-Allow-Headers': 'X-Requested-With,Content-Type',
// 'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS',
'Access-Control-Allow-Methods': 'POST,GET',
'Content-Type': 'application/json; charset=utf-8',
})
}
req.method === 'OPTIONS' ? res.status(204).end() : next()
})
// middleware .body parser
app.use(client(), bodyParser.json(), bodyParser.urlencoded({extended: false}));
// Wasm方式
app.get('/enc', (request, response) => {
commonHandle.handleCommonGet(request, response)
});
app.post('/enc', (request, response) => {
encryptHandle.handleWasmEncrypt(request, response)
});
// PcWasm方式
app.get('/pc_wasm', (request, response) => {
commonHandle.handleCommonGet(request, response)
});
app.post('/pc_wasm', (request, response) => {
encryptHandle.handleWasmEncrypt(request, response)
});
// Pc algorithm
app.get('/pc', (request, response) => {
commonHandle.handleCommonGet(request, response)
});
app.post('/pc', (request, response) => {
encryptHandle.handlePcEncrypt(request, response)
});
// App algorithm
app.get('/app', (request, response) => {
commonHandle.handleCommonGet(request, response)
});
app.post('/app', (request, response) => {
encryptHandle.handleAppEncrypt(request, response)
});
// start api server
const port = parsePort(3000)
app.listen(process.env.PORT || port, () => {
// console.info(`server now listening at port ${port}`);
console.info(`server running @ http://0.0.0.0:${process.env.PORT || port}`)
});