const Koa = require('koa') const { Client } = require('pg') const app = new Koa() const client = new Client({ user: "postgres", password: "postgres", database: "example", host: "db", port: 5432 }) client.connect() app.use(async ctx => { ctx.response.type = 'html' let id = ctx.request.query.id || 1 let sql = `SELECT * FROM "user" WHERE "id" = ${id}` const res = await client.query(sql) ctx.body = `
id | ${res.rows[0].id} |
---|---|
name | ${res.rows[0].name} |
score | ${res.rows[0].score} |