Created
April 16, 2014 22:44
-
-
Save ghabs/10939935 to your computer and use it in GitHub Desktop.
Express API tutorial
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
var express = require('express'); | |
var app = express(); | |
app.use(express.bodyParser()); | |
var headlines = [ | |
{ author : 'John Smith', text : "You will not believe what this child does next."}, | |
{ author : 'Jane Doe', text : "It started as a protest, then turned into a party. Thats not even the interesting part."}, | |
{ author : 'Alice Example', text : "Can we squeeze two hundred jellybeans in one package? Yah probably."}, | |
{ author : 'Bob Allan', text : "This famous celebrity is opposed to something you are too."} | |
]; | |
app.get('/headlines', function(req, res){ | |
res.json(headlines); | |
}); | |
app.get('/headlines/:id', function(req, res) { | |
var q = headlines[req.params.id]; | |
res.json(q); | |
}); | |
app.post('/headlines', function(req, res) { | |
var newHeadline = { | |
author : req.body.author, | |
text : req.body.text | |
} | |
headlines.push(newHeadline); | |
res.json(204); | |
}); | |
app.delete('/headline/:id', function(req, res) { | |
headlines.splice(req.params.id, 1); | |
res.json(204); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment