https://github.com/path-check/safeplaces-backend-lib
SafePlaces Backend server Library that is used to power the Backend, Ingest, and Translation API's.
https://github.com/path-check/safeplaces-backend-lib
express helper non-critical
Last synced: about 2 months ago
JSON representation
SafePlaces Backend server Library that is used to power the Backend, Ingest, and Translation API's.
- Host: GitHub
- URL: https://github.com/path-check/safeplaces-backend-lib
- Owner: Path-Check
- License: mit
- Created: 2020-06-22T16:56:45.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-13T12:01:11.000Z (over 3 years ago)
- Last Synced: 2024-04-24T06:07:09.043Z (about 2 years ago)
- Topics: express, helper, non-critical
- Language: JavaScript
- Homepage:
- Size: 137 KB
- Stars: 0
- Watchers: 8
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SafePlaces Server Library
The SafePlaces Server library is a pre-built Express server used across all SafePlaces services. A full example of how to integrate this library into an existing service can be found in the `sample/` directory.
## Environment Variables
A single environment variable is required to configure the port that the server should run on.
```
EXPRESSPORT=3000
```
## Installation and Usage
Install with npm or yarn
```
npm install @pathcheck/safeplaces-server
-or-
yarn add @pathcheck/safeplaces-server
```
Once installed, create an `app.js` file at the root level and add the following:
```
const path = require('path');
const config = {
port: process.env.EXPRESS_PORT || '3000',
bind: '127.0.0.1',
appFolder: path.join(__dirname, 'app'),
wrapAsync: (asyncFn, validate = false) => {
return (req, res, next) => {
if (validate) {
// Do some sort of validation here.
}
asyncFn(req, res, next).catch(next);
};
},
};
const server = require('@pathcheck/safeplaces-server')(config);
const enforcer = require('./app/lib/auth');
server.setupAndCreate();
module.exports = server;
```
### Options
The following are a list of options that can be passed to the server for further customization:
- `port`: port to run the server on
- `bind`: bind address
- `appFolder`: folder where your application logic exists
- `wrapAsync`: method for handling async/await methods within routes. Will also handle validation.
### Starting the server
Below is example code of how to start the server (normally located in `bin/www`).
```
#!/usr/bin/env node
require('dotenv').config()
const server = require('../app');
server.start()
```
### Routes
Below is example code of how to add a route to the server.
```
const { router } = require('../../../app');
const controller = require('./controller');
router.post(
'/signup',
router.wrapAsync(
async (req, res) => await controller.signupAction(req, res),
true,
),
);
```