-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (47 loc) · 1.17 KB
/
Copy pathindex.js
File metadata and controls
52 lines (47 loc) · 1.17 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
const PORT = process.env.PORT || 4300,
express = require('express'),
bodyParser = require('body-parser'),
axios = require('axios'),
vk = require('./src/execute')
const tokens = {}
const app = express()
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.all('/method/:method', async (req, res) => {
try {
const { method, access_token, ...params } = { ...req.body, ...req.params, ...req.query }
if (access_token === undefined) {
res.json({
error: 'No access_token',
body: req.body,
params: req.params,
query: req.query,
})
return
}
if (tokens[access_token] === undefined) {
tokens[access_token] = vk(access_token)
}
const response = await tokens[access_token].execute(method, params)
if (response.error_code !== undefined) {
res.json({
error: {
error_code: response.error_code,
error_msg: response.error_msg,
request_params: Object.entries(params).map(([key, value]) => ({
key,
value,
})),
},
})
} else {
res.json({
response,
})
}
} catch (e) {
console.error(e)
res.json(e)
}
})
app.listen(PORT, () => console.log(`Server started on ${PORT} port`))