the-forest/server/routes/api.js

34 lines
890 B
JavaScript

const { graphFromList, graphQueryString } = require('../graphStuff.js');
const sqlite = require('better-sqlite3');
const db = new sqlite('the_big_db.db', { verbose: console.log });
const express = require('express');
const app = express.Router();
const page_routes = require('./pages.js');
app.use(page_routes);
const user_routes = require('./users.js');
app.use(user_routes);
const live_connection_routes = require('./live.js');
app.use(live_connection_routes);
app.get('/graph', (req, res) => {
try {
const rows = db.prepare(graphQueryString).all();
const parsedRows = rows.map((row) => {
let newRow = row;
newRow.contents = JSON.parse(row.contents);
return newRow;
});
const graph = graphFromList(parsedRows);
res.status(200).json(graph);
} catch (error) {
console.log("error:", error);
res.status(500).json({"error": error});
}
});
module.exports = app;