Created
December 27, 2017 07:34
-
-
Save hashrock/7711ad6901f8b0e70a48b3305a6cdcc9 to your computer and use it in GitHub Desktop.
Koa my boilerplate
This file contains hidden or 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 path = require('path'); | |
const koaBody = require('koa-body'); | |
const Koa = require('koa'); | |
const app = module.exports = new Koa(); | |
const datastore = require('nedb-promise') | |
const _ = require('koa-route'); | |
const serve = require('koa-static'); | |
var gsjson = require('google-spreadsheet-to-json'); | |
app.use(koaBody()); | |
let DB = datastore({ | |
filename: 'db.json', | |
autoload: true | |
}) | |
app.use(_.get('/items', async function(ctx) { | |
ctx.body = await DB.find({}) | |
})); | |
app.use(_.get('/menus', async function(ctx){ | |
try{ | |
var result = await gsjson({ | |
spreadsheetId: 'SPREADSHEET_ID', | |
// other options... | |
}) | |
ctx.body = result; | |
}catch(e){ | |
console.log(e) | |
ctx.body = e; | |
} | |
})); | |
app.use(_.post('/items', async function(ctx) { | |
var item = ctx.request.body | |
await DB.insert(item) | |
ctx.body = {item: item} | |
})); | |
app.use(serve('.')); | |
if (!module.parent) app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment