Skip to content

Instantly share code, notes, and snippets.

@hashrock
Created December 27, 2017 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hashrock/7711ad6901f8b0e70a48b3305a6cdcc9 to your computer and use it in GitHub Desktop.
Save hashrock/7711ad6901f8b0e70a48b3305a6cdcc9 to your computer and use it in GitHub Desktop.
Koa my boilerplate
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