# @syncthetic/rest-on-express

> Easily build and manage a RESTful API using Express and MongoDB

Latest version **1.0.3** (published 2019-01-10) · GNU General Public License v3.0 license · 0 weekly downloads

## Install

```sh
npm install @syncthetic/rest-on-express
pnpm add @syncthetic/rest-on-express
yarn add @syncthetic/rest-on-express
bun add @syncthetic/rest-on-express
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2019-01-10 |
| First published | 2019-01-08 |
| Weekly downloads | 0 |
| License | GNU General Public License v3.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 6.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Justin Bess |
| Maintainers | besstech |

## Links

- npm: https://www.npmjs.com/package/@syncthetic/rest-on-express
- Repository: https://github.com/Syncthetic/RestOnExpress
- Homepage: https://github.com/Syncthetic/RestOnExpress#readme
- Issues: https://github.com/Syncthetic/RestOnExpress/issues
- npm.io page: https://npm.io/package/@syncthetic/rest-on-express

## Dependencies (3)

- [express](https://npm.io/package/express.md) ^4.16.4
- [mongodb](https://npm.io/package/mongodb.md) ^3.1.10
- [body-parser](https://npm.io/package/body-parser.md) ^1.18.3

## Recent versions

- 1.0.3 (latest) — 2019-01-10
- 0.0.2 — 2019-01-08
- 0.0.1 — 2019-01-08

## README

# RestOnExpress
> Easily build and manage a RESTful API using Express and MongoDB.

Configuration settings are pulled from the environment.

| Environment Variable | Value                                         | Default Value |
|-                     | -                                             | -             |
| `ROE_DB_CONNECTION`  | `<mongodb+srv://<username>:<password>@<host>` |               |
| `ROE_DB_NAME`        | `<name of the datbase>`                       |               |
| `ROE_COLLECTIONS`    | `<collection1:collection2:...>`               |               |
| `ROE_PORT`           | `<application listening port>`                | `3000`        |
| `ROE_API_BASE`       | `<the base reference to the API>`             | `/api`        |
| `STANDALONE`         | `boolean true if not an npm module`           | `false` |
> Note that the first collection in the `ROE_COLLECTIONS` environment variable will be instantiated first. To use other collections in your resources, call the `coll(name: string): collection` method

# Getting started as an NPM Module
Install the package
`npm i @syncthetic/rest-on-express --save`
import the package, set the route directory, and start the service
```javascript
const restOnExpress = require('@syncthetic/rest-on-express/app')
restOnExpress.set_route_directory(__dirname + '/routes')
restOnExpress.start_api()
```

The route directory should point to the routes to be used via the Express app.

# Getting started as repository
To get started, clone the repository
`git clone https://github.com/Syncthetic/RestOnExpress`

Install dependant packages `npm install`

Set the environment variable `ROE_STANDALONE` to any value.

The API resources should be configured inside the `routes/index.js` file.
Configure resources to point to the file which handles it's logic.
These files should be stored and nested inside the `/routes` directory. I would encourage the use of descriptive path names.
```javascript
// routes/index.js
router.use('/users', require('./users') // load routes/users/index.js
router.use('/user', require('./user')   // load routes/user/index.js
```
> Since resources are in noun form, and can have singular and plural form associated with it, such as `user` and `users` resources, you may choose to have both singular and plural forms in one directory such as `routes/users/plural.js` and `routes/users/singular.js`

If your environment variables are set, start the API service with `node app.js`
If desired, enter the environment variables when invoking the application
```
ROE_DB_CONNECTION="mongodb+srv://myusername:secretpass@myhost.com" \
ROE_DB_NAME="orders" \
ROE_COLLECTIONS="products" \
node app.js
```

Sending API requests: `http://localhost:<ROE_PORT>/<ROE_API_BASE>/<desired route path>`
> i.e,
>`GET http://localhost:3000/api/users` or 
>`PUT http://localhost:3000/api/user`

Example for a simple `user` resource

```javascript
// routes/user/index.js

var router = require('express').Router();

router.post("/", (request, response) => {
    collection.insertOne(request.body, (error, result) => {
        if(error) {
            return response.status(500).send(error);
        }
        response.status(201).send(result.result);
    })
})

// GET /api/user/:id
router.get("/:id", (request, response) => {
    collection.findOne({ "userId": request.params.id }, (error, result) => {
        if(error) {
            return response.status(500).send(error);
        }
        response.send(result.result);
    })
})

// PUT /api/user/:id
router.put("/:id", (request, response) => {
    collection.updateOne(
        { "userId" : request.params.id },
        { $set: request.body },
        { upsert: true},
        (error, result) => {
            if (error) { return response.status(500).send(error) }
            if (result.result.nModified === 1 && result.result.n === 1) response.status(200).send(result.result)
            else if ( result.result.upserted ) response.status(201).send(result.result)
            else if (result.result.n === 1 && result.result.nModified === 0 ) response.status(304).send(result.result)
        }
     )
})

// DELETE /api/user/:id
router.delete("/:id", (request, response) => {
    collection.deleteOne({ "userId": request.params.id }, (error, result) => {
        if(error) {
            return response.status(500).send(error);
        }
        response.send(result.result);
    })
})

module.exports = router; 
```

---
_Source: https://npm.io/package/@syncthetic/rest-on-express · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
