Here's a quick take. I'm sure it can be cleaned up further. I've also taken the liberty of fixing the delete before post id bug.
express = require 'express'
bodyParser = require 'body-parser'
server = express()
server.use bodyParser()
books = [
{ id: 1, title: "Microsoft Visual C# 2012", author: "John Sharp" }
{ id: 2, title: "C# 5.0 in a nutshell", author: "Joseph Albahari" }
{ id: 3, title: "C# in Depth, 3rd Edition", author: "Jon Skeet" }
{ id: 4, title: "Pro ASP.NET MVC 5", author: "Adam Freeman" }
]
findBook = (req, res, cb) ->
book = books.filter (b) -> b.id is +req.params.id
if book.length
cb book[0]
else
res.statusCode = 404
res.send "No book with ID = #{req.params.id}"
server.get "/api/books", (req, res) ->
res.send books
server.get "/api/books/:id", (req, res) ->
findBook req, res, (book) ->
res.send book
server.post "/api/books", (req, res) ->
book = req.body
book.id = 1 + Math.max.apply Math, books.map (b) -> b.id
books = books.concat [book]
res.send 200
server.put "/api/books/:id", (req, res) ->
findBook req, res, (book) ->
updates = req.body
['title', 'author'].forEach (f) -> book[f] = updates[f]
res.send 200
server.delete "/api/books/:id", (req, res) ->
findBook req, res, (book) ->
books = books.filter (b) -> b.id isnt book.id
res.send 200
server.listen 3000
console.log "server listening on localhost:3000" netsh http add urlacl url=http://+:8080/ user=DOMAIN\username
You don't need attributes based routing in this example. Convention based routing is built into WebAPI.For the Delete method you can return an HttpStatusCode instead of throwing an exception.
public IHttpActionResult Delete(int Id)
{
var result = (from b in ourbooks
where b.Id == Id
select b).FirstOrDefault();
ourbooks.Remove(result);
return StatusCode(HttpStatusCode.Accepted);
}
Also check out the OWIN self host tutorial [2].[1] https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-owi...
[2] http://www.asp.net/web-api/overview/hosting-aspnet-web-api/u...
var resp = new HttpResponseMessage(HttpStatusCode.Created);
throw new HttpResponseException(resp);Tho, http://requestb.in/ is a superb tool as well as https://www.runscope.com if you need more power.
p.s. this one too : http://httpbin.org/
-bowerbird