assets/api/index.js

47 lines
1.3 KiB
JavaScript

import express from 'express'
import sqlite3 from 'sqlite3'
sqlite3.verbose()
const app = express()
const db = new sqlite3.Database('../db')
const port = 3102
process
.on('unhandledRejection', console.log)
.on('uncaughtException', console.log)
db.serialize(async () => {
app.get('/ETH/:date', async (req, res) => {
const date = new Date(req.params.date)
if(date.toString() == 'Invalid Date') {
res.send('Invalid Date')
return
}
db.each(`SELECT * FROM assets WHERE asset = 'ETH' AND date = ${Number(date)} LIMIT 1`, (err, row) => {
res.send(row.price.toString())
}, (err, count) => {
if(count == 0) {
res.send('Invalid Date')
}
})
})
app.get('/TRX/:date', async (req, res) => {
const date = new Date(req.params.date)
if(date.toString() == 'Invalid Date') {
res.send('Invalid Date')
return
}
db.each(`SELECT * FROM assets WHERE asset = 'TRX' AND date = ${Number(date)} LIMIT 1`, (err, row) => {
res.send(row.price.toString())
}, (err, count) => {
if(count == 0) {
res.send('Invalid Date')
}
})
})
app.listen(port, () => {
console.log(`App listening on port ${port}`)
})
})