-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
71 lines (64 loc) · 1.51 KB
/
server.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
import express from 'express'
import cors from 'cors'
import http from 'http'
import bodyParser from 'body-parser'
import config from './config'
import { getTransactionsByAddress, addTransaction } from './transactions'
import { getCurrency } from './currencies'
const app = express()
app.use(bodyParser.json())
if (config.stage === 'dev') {
app.use(cors())
}
app.set('json spaces', 2)
const server = http.Server(app)
/**
* Get transactions for address
*/
app.get('/api/transactions/:address', (req, res, next) => {
getTransactionsByAddress(req.params.address.replace("xrb_", "nano_"))
.then(rows => {
res.json({
success: true,
transactions: rows
})
})
.catch(err => {
console.error('Error in `/transactions/:address` ===', err)
next(err)
})
})
/**
* Add transaction
*/
app.post('/api/transaction', (req, res, next) => {
console.log(req.body)
addTransaction(req.body)
.then(result => {
res.json({
success: true,
txId: result.id
})
})
.catch(err => {
console.error('Error in `post` to `transaction` ===', err)
next(err)
})
})
/**
* Get currency price by code
*/
app.get('/api/currencies/:currencyCode', (req, res, next) => {
getCurrency(req.params.currencyCode)
.then(rows => {
res.json({
success: true,
currency: rows
})
})
.catch(err => {
console.error('Error in `/currencies/:currencyCode` ===', err)
next(err)
})
})
server.listen(config.serverPort)