{"id":20494860,"url":"https://github.com/apiko-dev/meteor-json-routes","last_synced_at":"2025-08-17T07:07:08.909Z","repository":{"id":88210021,"uuid":"96790662","full_name":"apiko-dev/meteor-json-routes","owner":"apiko-dev","description":null,"archived":false,"fork":false,"pushed_at":"2017-07-12T12:47:07.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-01-16T06:12:10.274Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/apiko-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-10T15:05:06.000Z","updated_at":"2017-07-10T15:06:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"72c3060f-d305-49e2-9db5-f45c9189e067","html_url":"https://github.com/apiko-dev/meteor-json-routes","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apiko-dev%2Fmeteor-json-routes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apiko-dev%2Fmeteor-json-routes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apiko-dev%2Fmeteor-json-routes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apiko-dev%2Fmeteor-json-routes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apiko-dev","download_url":"https://codeload.github.com/apiko-dev/meteor-json-routes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242076655,"owners_count":20068234,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T17:43:31.964Z","updated_at":"2025-03-05T17:48:18.521Z","avatar_url":"https://github.com/apiko-dev.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jss:json-routes\n\n### Note\n\nFork of `simple:json-routes` (\u003chttps://atmospherejs.com/simple/json-routes\u003e) that doesn't conflicts with `ostrio:files`.\n\n----\n\nThe simplest bare-bones way to define server-side JSON API endpoints, without\nany extra functionality. Based on [connect-route].\n\n## Example\n\n```js\nJsonRoutes.add(\"get\", \"/posts/:id\", function (req, res, next) {\n  var id = req.params.id;\n\n  JsonRoutes.sendResult(res, {\n    data: Posts.findOne(id)\n  });\n});\n```\n\n## API\n\n### JsonRoutes.add(method, path, handler)\n\nAdd a server-side route that returns JSON.\n\n- `method` - The HTTP method that this route should accept: `\"get\"`, `\"post\"`,\n  etc. See the full list [here][connect-route L4]. The method name is\n  case-insensitive, so `'get'` and `'GET'` are both acceptable.\n- `path` - The path, possibly with parameters prefixed with a `:`. See the\n  example.\n- `handler(request, response, next)` - A handler function for this route.\n  `request` is a Node request object, `response` is a Node response object,\n  `next` is a callback to call to let the next middleware handle this route. You\n  don't need to use this normally.\n\n### JsonRoutes.sendResult(response, options)\n\nReturn data fom a route.\n\n- `response` - Required. The Node response object you got as an argument to your handler function.\n- `options.code` - Optional. The status code to send. `200` for OK, `500` for internal error, etc. Default is 200.\n- `options.headers` - Optional. Dictionary of headers to send back.\n- `options.data` - Optional. The data you want to send back. This is serialized to JSON with content type `application/json`. If `undefined`, there will be no response body.\n\n### Errors\n\nWe recommend that you simply throw an Error or Meteor.Error from your handler function. You can then attach error handling middleware that converts those errors to JSON and sends the response. Here's how to do it with our default error middleware:\n\n```js\nJsonRoutes.ErrorMiddleware.use(\n  '/widgets',\n  RestMiddleware.handleErrorAsJson\n);\n\nJsonRoutes.add('get', 'widgets', function () {\n  var error = new Meteor.Error('not-found', 'Not Found');\n  error.statusCode = 404;\n  throw error;\n});\n```\n\n### JsonRoutes.setResponseHeaders(headerObj)\n\nSet the default headers used by `JsonRoutes.sendResult` for the response. Default value is:\n\n```js\n{\n  \"Cache-Control\": \"no-store\",\n  \"Pragma\": \"no-cache\"\n}\n```\n\nYou can pass additional headers directly to `JsonRoutes.sendResult`\n\n## Adding Middleware\n\nIf you want to insert connect middleware and ensure that it runs before your\nREST route is hit, use `JsonRoutes.Middleware`.\n\n```js\nJsonRoutes.Middleware.use(function (req, res, next) {\n  console.log(req.body);\n  next();\n});\n```\n\n## Creating Middleware Packages\n\nOnce you've created an awesome piece of reusable middleware and you're ready to\nshare it with the world, you should make it a Meteor package so it can be easily\nconfigured in any JSON Routes API. There are only two simple requirements.\nActually, they're just very strong recommendations. Nothing will explode if you\ndon't follow these guidelines, but doing so should promote a much cleaner\nmiddleware ecosystem.\n\nEach middleware package should define a single middleware function and add it\nto `RestMiddleware` namespace:\n\n```js\nRestMiddleware.someMiddlewareFunc = function (req, res, next) {\n  // Do some awesome middleware stuff here\n};\n\nRestMiddleware.someMiddlewareErrorFunc = function (err, req, res, next) {\n  // Do some awesome middleware error handling here\n};\n```\n\nAlternatively, you could publish a pure NodeJS middleware package to NPM, and you will be able to require it and use it in your Meteor package or app.\n\n### Auth Middleware\n\n- By convention, any middleware you create that parses the request to find an authentication token should then save that token on `req.authToken`. See `simple:rest-bearer-token-parser` for an example.\n- By convention, any middleware you create that determines a user ID should save that ID on `req.userId`. See `simple:authenticate-user-by-token` for an example.\n\n## Change Log\n\n#### 2.1.0\n\n- Fix issue #82 by wrapping the middlewares in a fiber.\n\n#### 2.0.1\n\n- Increase request size limit to 50 MB, PR #88, Issue #75\n\n#### 2.0.0\n\n- `JsonRoutes.sendResult` function signature has changed to `(response, options)` and you can now pass in headers. See documentation.\n- `JsonRoutes.sendError` no longer exists. Throw the error instead, and use error handling middleware to parse and return it.\n- `connect` dependency updated to 2.30.2\n- `RestMiddleware` object is exported for packages to add middleware functions to\n- `JsonRoutes.ErrorMiddleware.use` is a new function that can be called to add error handling middleware, globally or per route, to ensure it is added last.\n\n#### 1.0.4\n\n- Allow case-insensitive method names to be passed as the first param to `JsonRoutes.add()` (e.g., `JsonRoutes.add('get',...)` and `JsonRoutes.add('GET',...)` are both acceptable)\n- Add `JsonRoutes.sendError` with automatic parsing of error objects.\n- Catch handler errors and automatically send a response. Look for `statusCode` and `data` properties on thrown errors.\n- Add `JsonRoutes.Middleware` to eventually replace `JsonRoutes.middleWare` (since 'middleware' is one word)\n- Fix Connect middleware deprecation error https://github.com/stubailo/meteor-rest/issues/18\n\n#### 1.0.3\n\nAdd `JsonRoutes.middleWare` for adding middleware to the stack\n\n[connect-route]: https://github.com/baryshev/connect-route\n[connect-route L4]: https://github.com/baryshev/connect-route/blob/06f92e07dc8e4690f7f788df39b37b5db4b06f90/lib/connect-route.js#L4\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapiko-dev%2Fmeteor-json-routes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapiko-dev%2Fmeteor-json-routes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapiko-dev%2Fmeteor-json-routes/lists"}