simple:json-routes

v1.0.0Published 7 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

json-routes

The simplest bare-bones way to define server-side JSON API endpoints, without any extra functionality. Based on connect-route.

Example

1JsonRoutes.add("get", "/posts/:id", function (req, res, next) {
2  var id = req.params.id;
3
4  JsonRoutes.sendResult(res, 200, Posts.findOne(id));
5});

API

JsonRoutes.add(method, path, handler)

Add a server-side route that returns JSON.

  • method - The HTTP method that this route should accept: "get", "post",

etc. See the full list here.

  • path - The path, possibly with parameters prefixed with a :. See the

example.

  • handler(request, response, next) - A handler function for this route.

request is a Node request object, response is a Node response object, next is a callback to call to let the next middleware handle this route. You don't need to use this normally.

JsonRoutes.sendResult(response, code, data)

Return data fom a route.

  • response - the Node response object you got as an argument to your handler

function.

  • code - the status code to send. 200 for OK, 500 for internal error, etc.
  • data - the data you want to send back, will be sent as serialized JSON with

content type application/json.