Last active
November 8, 2017 18:29
-
-
Save pokorson/4d5e301b14a52b84f845295350b78101 to your computer and use it in GitHub Desktop.
Simple read/create server with in-memory storage for nodeschool React workshop
This file contains 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 koa = require('koa'); | |
const cors = require('kcors'); | |
const uuid = require('uuid/v1'); | |
const bodyParser = require('koa-bodyparser'); | |
const app = new koa(); | |
let tweets = {}; | |
app.use(cors()); | |
app.use(bodyParser()); | |
app.use(async ctx => { | |
if (ctx.path === '/tweets') { | |
const { user } = ctx.request.query; | |
if (!user) { | |
ctx.status = 401; | |
ctx.body = 'no user provided'; | |
} else { | |
ctx.status = 200; | |
ctx.body = { | |
tweets: tweets[user], | |
}; | |
} | |
} else if (ctx.path === '/createTweet') { | |
const { tweet } = ctx.request.body; | |
const { user } = ctx.request.query; | |
if (!user || (!tweet && !tweet.title)) { | |
ctx.status = 400; | |
ctx.body = 'bad request'; | |
} else { | |
if (!tweets[user]) { | |
tweets[user] = []; | |
} | |
tweet['uuid'] = uuid(); | |
tweets[user] = [...tweets[user], tweet]; | |
ctx.status = 201; | |
ctx.body = tweet; | |
} | |
} else { | |
ctx.body = 'hello'; | |
ctx.status = 200; | |
} | |
}); | |
app.listen(3001); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment