Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
145 lines (134 loc) · 4.81 KB
/
Copy pathserver.js
File metadata and controls
145 lines (134 loc) · 4.81 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const express = require('express')
const next = require('next')
const { parse } = require('url')
const { join } = require('path')
const dev = process.env.NODE_ENV !== 'production'
const axios = require('axios')
const qs = require('qs')
const app = next({dev})
const handle = app.getRequestHandler()
let port = 3000
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const { getPartners } = require('./server/data.js')
const { getUsers, addUser, deleteUser, register, login } = require('./server/user.js')
if (process.env.NODE_ENV === 'dev') {
if (!process.env.LOCAL_API) {
global.baseApiurl = 'https://api-v2-dev-d981hkl2m.bitmart.com/api/v3'
global.baseNewApiurl = 'https://api-v2-dev-d981hkl2m.bitmart.com/cal'
global.baseAssetApiurl = 'https://api-v2-dev-d981hkl2m.bitmart.com/asset'
global.baseMainurl = 'https://api-v2-dev-d981hkl2m.bitmart.com'
global.apihost = 'api-v2-dev-d981hkl2m.bitmart.com'
predomain = 'web-frontend-v2-dev-d981hkl2m.'
} else {
global.baseApiurl = `http://${process.env.LOCAL_API}/api/v3`
global.baseNewApiurl = `http://${process.env.LOCAL_API}/cal`
global.baseAssetApiurl = `http://${process.env.LOCAL_API}/asset`
global.baseMainurl = `http://${process.env.LOCAL_API}`
global.apihost = 'api-v2-dev-d981hkl2m.bitmart.com'
predomain = 'web-frontend-v2-dev-d981hkl2m.'
}
} else if (process.env.NODE_ENV === 'testing') {
if (!process.env.LOCAL_API) {
global.baseApiurl = 'https://api-v2-testing-d8pvw98nl.bitmart.com/api/v3'
global.baseNewApiurl = 'https://api-v2-testing-d8pvw98nl.bitmart.com/cal'
global.baseAssetApiurl = 'https://api-v2-testing-d8pvw98nl.bitmart.com/asset'
global.baseMainurl = 'https://api-v2-testing-d8pvw98nl.bitmart.com'
global.apihost = 'api-v2-testing-d8pvw98nl.bitmart.com'
predomain = 'web-frontend-v2-testing-d981hkl2m.'
} else {
global.baseApiurl = `http://${process.env.LOCAL_API}/api/v3`
global.baseNewApiurl = `http://${process.env.LOCAL_API}/cal`
global.baseAssetApiurl = `http://${process.env.LOCAL_API}/asset`
global.baseMainurl = `http://${process.env.LOCAL_API}`
global.apihost = 'api-v2-testing-d8pvw98nl.bitmart.com'
predomain = 'web-frontend-v2-testing-d981hkl2m.'
}
} else {
if (!process.env.LOCAL_API) {
global.baseApiurl = 'https://api.bitmart.com/api/v3'
global.baseNewApiurl = 'https://api.bitmart.com/cal'
global.baseAssetApiurl = 'https://api.bitmart.com/asset'
global.baseMainurl = 'https://api.bitmart.com'
global.apihost = 'api.bitmart.com'
predomain = 'www.'
} else {
global.baseApiurl = `http://${process.env.LOCAL_API}/api/v3`
global.baseNewApiurl = `http://${process.env.LOCAL_API}/cal`
global.baseAssetApiurl = `http://${process.env.LOCAL_API}/asset`
global.baseMainurl = `http://${process.env.LOCAL_API}`
global.apihost = 'api.bitmart.com'
predomain = 'www.'
}
}
(async () => {
await app.prepare()
const server = express()
server.use(bodyParser.raw())
server.use(bodyParser.json())
server.use(
bodyParser.urlencoded({
extended: true,
})
)
server.use(cookieParser())
server.use((req, res, next) => {
req.res = res
res.req = req
if (req.url.startsWith('/api')) {
if (req.method === 'GET') {
const url_parts = parse(req.url, true)
req.body = url_parts.query
}
req.headers.host = global.apihost
}
next()
})
// api 接口
server.get('/api/partners', getPartners)
server.get('/api/get_btc_initial_price', (req, res) => {
axios({
timeout: 10000,
method: 'GET',
url: `http://api.bitcoincharts.com/v1/trades.csv?symbol=bitstampUSD`,
data: req.body,
headers: {
'Content-Type': 'text/plain'
},
transformRequest: (obj) => {
return qs.stringify(obj)
},
})
.then((ret) => {
ret.data.success = true
return res.json(ret.data)
})
.catch((e) => {
return res.status(500).send({
code: -9999,
data: 'node error',
msg: e.message,
success: false,
})
})
})
//test for springboot project api, test ok
server.get('/api/getusers', getUsers)
server.get('/api/adduser', addUser)
server.get('/api/deleteuser', deleteUser)
server.get('/api/register', register)
server.get('/api/login', login)
server.get('*', (req, res) => {
const parsedUrl = parse(req.url, true)
// 这几个特殊文件不需要加static路径访问
const rootStaticFiles = ['/robots.txt', '/sitemap.xml', '/favicon.ico']
if (rootStaticFiles.indexOf(parsedUrl.pathname) > -1) {
const path = join(__dirname, 'static', parsedUrl.pathname)
app.serveStatic(req, res, path)
} else {
handle(req, res, parsedUrl)
}
})
await server.listen(3009)
console.log('> Ready on http://localhost:3009')
})()