dervland-data/index.js
2025-05-19 02:05:54 -04:00

41 lines
905 B
JavaScript

import express from 'express';
import { initDb } from './db.js';
import bodyParser from 'body-parser';
const app = express()
const port = process.env.port || 5000
const jsonParser = bodyParser.json()
app.use(jsonParser);
app.post('/', async (req, res) => {
var db = await initDb();
var ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
var { action } = req.body;
action = action + '';
await db.run('DELETE FROM action WHERE addr = ? AND action = ?', [
ip,
action
]);
await db.run('INSERT INTO action (addr, action) VALUES (?, ?)', [
ip,
action
]);
let output = await db.all('SELECT * FROM action WHERE action = ?', [
action
])
console.log(`${output.length} people triggered ${action}.`);
res.send({
ip,
action,
count: output.length
});
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})