Skip to content

Instantly share code, notes, and snippets.

@ghabs
Created April 16, 2014 22:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghabs/10939935 to your computer and use it in GitHub Desktop.
Save ghabs/10939935 to your computer and use it in GitHub Desktop.
Express API tutorial
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