https://github.com/jalik/js-rest-server
A REST API builder based on Express.
https://github.com/jalik/js-rest-server
api builder expressjs rest server
Last synced: about 1 year ago
JSON representation
A REST API builder based on Express.
- Host: GitHub
- URL: https://github.com/jalik/js-rest-server
- Owner: jalik
- License: mit
- Created: 2018-03-12T04:55:10.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T01:33:23.000Z (over 3 years ago)
- Last Synced: 2024-04-14T06:18:24.819Z (about 2 years ago)
- Topics: api, builder, expressjs, rest, server
- Language: JavaScript
- Size: 1.72 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# @jalik/rest-server

[](https://travis-ci.com/jalik/js-rest-server)


[](https://github.com/jalik/js-rest-server/issues)

## Introduction
This is a REST API server based on the excellent [Express](https://expressjs.com/) web server.
The main goal is to simplify the management of any APIs.
Note that this server is independent and cannot work as a middleware in an existing express application, it needs to run on a dedicated port.
## Why using this ?
- The server restarts automatically when a route is added or removed, or if the port changed
- Routes are objects which can be used to generate API documentation
- You can use any express middleware as it is based on it
## Creating a REST API
When creating an API, you must at least define a `method`, a `path` and a `handler`.
Note that the handler is an Express handler (see [https://expressjs.com/en/starter/basic-routing.html]()).
Since you may have a lot of APIs, it's recommended to put them in separate files like below.
```js
import { Route } from '@jalik/rest-server';
const GetDateAPI = new Route({
cors: false,
method: 'GET',
path: '/v1/date',
handler(req, resp) {
resp.status(200).end(JSON.stringify({
date: new Date().toISOString()
}));
}
});
export default GetDateAPI;
```
## Creating a server
To serve the APIs you've created, you need a web server, so let see how to do that.
```js
import Server from '@jalik/rest-server';
// Setup server
const server = new Server({
port: 3001,
// Automatically restart the server
// if an API has been added or removed.
restartOnChange: true,
});
// Add routes
// ... server.addRoute(route);
// ... server.addRoute(route);
// Finally start the server
server.start();
```
## Adding a middleware
```js
import Server from '@jalik/rest-server';
// Setup server
const server = new Server({
port: 3001
});
// Log request date, method and URL to the console
server.addMiddleware((req, resp, next) => {
console.log(`${new Date()} ${req.method} ${req.url}`);
next();
});
server.start();
```
## Enabling CORS
To enable CORS on a route, just set the `cors` option to `true` when creating a route,
and eventually define the `corsOptions` for more control.
```js
import { Route } from '@jalik/rest-server';
const AuthAPI = new Route({
cors: true,
corsOptions: {
origin: 'http://example.com'
},
method: 'POST',
path: '/auth',
handler(req, resp, next) {
// logic...
}
});
```
For more details, see https://expressjs.com/en/resources/middleware/cors.html#configuring-cors.
## Changelog
History of releases is in the [changelog](./CHANGELOG.md).
## License
The code is released under the [MIT License](http://www.opensource.org/licenses/MIT).