Created
May 6, 2021 03:22
-
-
Save hargup/a626376de1e68e217723faa1f18ca3a7 to your computer and use it in GitHub Desktop.
Server stackoverflow answers through a nodejs service. Use https://archive.org/details/stackover-sqlite.db to download the March 2021 dump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sqlite3 = require('sqlite3').verbose(); | |
const express = require("express") | |
const morgan = require('morgan') | |
const cors = require('cors') | |
const app = express() | |
app.use(express.json()) | |
app.use(cors()) | |
app.options('*', cors()) | |
app.use(morgan('tiny')) | |
let db = new sqlite3.Database('./so-dump.db', (err) => { | |
if (err) { | |
console.error(err.message); | |
} | |
console.log('Connected to the SO database.'); | |
}); | |
app.get("/api/stackoverflow.com/answers", async function(req, res) { | |
const questionId = decodeURIComponent(req.query.question_id); | |
db.all("SELECT * FROM posts where Id= (SELECT AcceptedAnswerId FROM posts where Id=$1)", | |
[questionId], | |
(err, rows) => { | |
if(rows.length > 0) { | |
res.send(rows[0]); | |
} else { | |
db.all("select * from posts where ParentId=$1 ORDER BY Score DESC LIMIT 1;", | |
[questionId], | |
(err2, rows2) => { | |
res.send(rows2[0]); | |
}) | |
} | |
}) | |
}) | |
app.get("/api/stackoverflow.com/questions", async function(req, res) { | |
const questionId = decodeURIComponent(req.query.question_id); | |
try{ | |
db.all("select * from posts where Id=$1;", [questionId], (err, rows) => { | |
res.send(rows[0]); | |
}) | |
} catch (err) { | |
res.status(404) | |
} | |
}) | |
port = process.env.port || process.env.PORT || 5000 | |
app.listen(port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment