{"id":16995191,"url":"https://github.com/jalik/js-rest-server","last_synced_at":"2025-04-05T18:40:32.355Z","repository":{"id":40766330,"uuid":"124835450","full_name":"jalik/js-rest-server","owner":"jalik","description":"A REST API builder based on Express.","archived":false,"fork":false,"pushed_at":"2023-01-06T01:33:23.000Z","size":1806,"stargazers_count":0,"open_issues_count":13,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-14T06:18:24.819Z","etag":null,"topics":["api","builder","expressjs","rest","server"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jalik.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-12T04:55:10.000Z","updated_at":"2021-06-05T00:49:16.000Z","dependencies_parsed_at":"2023-02-05T01:32:40.701Z","dependency_job_id":null,"html_url":"https://github.com/jalik/js-rest-server","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalik%2Fjs-rest-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalik%2Fjs-rest-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalik%2Fjs-rest-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalik%2Fjs-rest-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jalik","download_url":"https://codeload.github.com/jalik/js-rest-server/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247385348,"owners_count":20930590,"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":["api","builder","expressjs","rest","server"],"created_at":"2024-10-14T03:47:49.860Z","updated_at":"2025-04-05T18:40:32.335Z","avatar_url":"https://github.com/jalik.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @jalik/rest-server\n\n![GitHub package.json version](https://img.shields.io/github/package-json/v/jalik/js-rest-server.svg)\n[![Build Status](https://travis-ci.com/jalik/js-rest-server.svg?branch=master)](https://travis-ci.com/jalik/js-rest-server)\n![GitHub](https://img.shields.io/github/license/jalik/js-rest-server.svg)\n![GitHub last commit](https://img.shields.io/github/last-commit/jalik/js-rest-server.svg)\n[![GitHub issues](https://img.shields.io/github/issues/jalik/js-rest-server.svg)](https://github.com/jalik/js-rest-server/issues)\n![npm](https://img.shields.io/npm/dt/@jalik/rest-server.svg)\n\n## Introduction\n\nThis is a REST API server based on the excellent [Express](https://expressjs.com/) web server.\nThe main goal is to simplify the management of any APIs.\nNote that this server is independent and cannot work as a middleware in an existing express application, it needs to run on a dedicated port.\n\n## Why using this ?\n\n- The server restarts automatically when a route is added or removed, or if the port changed\n- Routes are objects which can be used to generate API documentation\n- You can use any express middleware as it is based on it\n\n## Creating a REST API\n\nWhen creating an API, you must at least define a `method`, a `path` and a `handler`.\nNote that the handler is an Express handler (see [https://expressjs.com/en/starter/basic-routing.html]()).\n\nSince you may have a lot of APIs, it's recommended to put them in separate files like below.\n\n```js\nimport { Route } from '@jalik/rest-server';\n\nconst GetDateAPI = new Route({\n  cors: false,\n  method: 'GET',\n  path: '/v1/date',\n  handler(req, resp) {\n    resp.status(200).end(JSON.stringify({ \n      date: new Date().toISOString()\n    }));\n  }\n});\n\nexport default GetDateAPI;\n```\n\n## Creating a server\n\nTo serve the APIs you've created, you need a web server, so let see how to do that.\n\n```js\nimport Server from '@jalik/rest-server';\n\n// Setup server\nconst server = new Server({\n  port: 3001,\n  // Automatically restart the server\n  // if an API has been added or removed.\n  restartOnChange: true,\n});\n\n// Add routes\n// ... server.addRoute(route);\n// ... server.addRoute(route);\n\n// Finally start the server\nserver.start();\n```\n\n## Adding a middleware\n\n```js\nimport Server from '@jalik/rest-server';\n\n// Setup server\nconst server = new Server({\n  port: 3001\n});\n\n// Log request date, method and URL to the console\nserver.addMiddleware((req, resp, next) =\u003e {\n  console.log(`${new Date()} ${req.method} ${req.url}`);\n  next();\n});\n\nserver.start();\n```\n\n## Enabling CORS\n\nTo enable CORS on a route, just set the `cors` option to `true` when creating a route,\nand eventually define the `corsOptions` for more control.\n\n```js\nimport { Route } from '@jalik/rest-server';\n\nconst AuthAPI = new Route({\n  cors: true,\n  corsOptions: {\n    origin: 'http://example.com'\n  },\n  method: 'POST',\n  path: '/auth',\n  handler(req, resp, next) {\n    // logic...\n  }\n});\n```\n\nFor more details, see https://expressjs.com/en/resources/middleware/cors.html#configuring-cors.\n\n## Changelog\n\nHistory of releases is in the [changelog](./CHANGELOG.md).\n\n## License\n\nThe code is released under the [MIT License](http://www.opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjalik%2Fjs-rest-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjalik%2Fjs-rest-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjalik%2Fjs-rest-server/lists"}