Skip to content

Instantly share code, notes, and snippets.

@hashrock hashrock/server.js
Created Dec 27, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.