【Express】REST

「RESTfulな*」とか書けばいいのだろうか、知らない。

method-overrideを使えば良いらしいので使う.


var bodyParser = require('body-parser');
var connect = require('connect');
var methodOverride = require('method-override');

// body-parserを上に。
app.use(bodyParser.urlencoded())
app.use(methodOverride(function(req, res){
  if (req.body && typeof req.body === 'object' && '_method' in req.body) {
    // look in urlencoded POST bodies and delete it
    var method = req.body._method
    delete req.body._method
    return method
  }
}));

// あとは適当に、req.bodyで取得可
app.get("/hoge", ~
app.post("/fuga", ~

app.put("/piyo", function(req, res){
	res.send(req.body);
});

app.delete("/hogehoge", ~
広告