How to create a self hosted API for development and testing
jeremymorgan.comAs long as you're just working on JavaScript code for the front end, wouldn't it make sense to write this in node? That way you can avoid context switching. I'd daresay this would be much simpler in JavaScript as well, to the point that a tutorial isn't really necessary.
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"You can allow non-administrator access using netsh[1] as an administrator. That way you don't need to run VS as admin.
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...
Is the normal way to returning "success" values (throwing an exception?)
var resp = new HttpResponseMessage(HttpStatusCode.Created);
throw new HttpResponseException(resp);Apiary is also a good option ( http://apiary.io).
i've been using apiary this week and it's been absolutely fantastic so far. Markdown goes in, API docs + mock live API comes out, even syncs with github.
it's definitely a great tool, it's really useful being able to chop and change in the early stages of designing an API without being committed in the code.
Does anyone know of a similar guide for Ruby? I'm an ASP.net developer and I'm trying to get some insight into other (perhaps quicker/easier) ways of doing exactly what this guide describes.
There was an REST server that responded with just a json you specify for a particular url, but god i can't recall the name.
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/
excellent! thanks!
-bowerbird
That's the first time I've heard that meaning of "self hosted." I was expecting something like a REST server that also acted as a client to another instance of itself.