Skip to content

Commit

Permalink
inclass -16
Browse files Browse the repository at this point in the history
inclass -16
  • Loading branch information
brendazhao authored Mar 7, 2017
1 parent 04f528c commit b5f39ca
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions inclass-16/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

const express = require('express')
const bodyParser = require('body-parser')

let articles=[
{"id":1, "author":"Scott", "text":"Text for Scott"},
{"id":2, "author":"Max", "text":"Text for Max"},
{"id":3, "author":"James", "text":"Text for James"}
]

const addArticle = (req, res) => {
console.log('Payload received', req.body)
let id=articles[articles.length-1]["id"]+1;
articles=[
...articles,
{
"id":id,
"author":"newAuthor",
"text" :req.body.text
}
]
res.send(articles[articles.length-1])
}


const getArticle = (req, res) => {
res.send(JSON.stringify(articles))
}

const hello = (req, res) => res.send({ hello: 'world' })

const app = express()
app.use(bodyParser.json())
app.post('/article', addArticle)
app.get('/', hello)
app.get('/articles', getArticle)

// Get the port from the environment, i.e., Heroku sets it
const port = process.env.PORT || 3000
const server = app.listen(port, () => {
const addr = server.address()
console.log(`Server listening at http://${addr.address}:${addr.port}`)
})

0 comments on commit b5f39ca

Please sign in to comment.