const app = require("express")(); const db = require("./db"); const port = process.env.PORT || 8080; const sep = `

`; function genListing(row) { return ` ${row.title || row.url}
${row.url}
`; } function searchBar(query = "") { return `
`; } function offsetButton(query, offset, limit, text) { return `${text}`; } app.get("/", (req, res) => { res.send(searchBar()); }); app.get("/search", (req, res) => { const query = req.query.q; const offset = +req.query.o || 0; const limit = +req.query.l || 50; const next = offsetButton(query, offset + limit, limit, "Next"); const prev = offsetButton(query, offset - limit, limit, "Prev"); const page = (offset / limit) + 1; db.all("SELECT url, title FROM page_search(?) ORDER BY rank LIMIT ?, ?", req.query.q, offset, limit, (err, rows) => { res.send(` ${searchBar(query)} ${prev} ${page} ${next} ${sep} ${rows.map(genListing).join(sep)} ${sep} ${prev} ${page} ${next} `); }); }); app.listen(port, () => { console.log(`Running at http://127.0.0.1:${port}/`); });