Skip to content

Commit

Permalink
Merge branch 'master' of bitbucket.org:beetech-engineers/server-mock-…
Browse files Browse the repository at this point in the history
…hook
  • Loading branch information
Icaro committed Feb 22, 2018
2 parents 69b9e5e + 853b520 commit b901c71
Show file tree
Hide file tree
Showing 9 changed files with 3,713 additions and 135 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=5000
HOST=http://localhost:5000
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.env
182 changes: 182 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# Bee Hook Server

`bee-hook` is a simple, zero-configuration command-line hook server. It is powerful enough for production usage, but it's simple and hackable enough to be used for testing, local development, and learning.
(yes, it's inspired on [http-server](https://www.npmjs.com/package/http-server))


## Installing globally

Installation via `npm`:

npm install bee-hook -g

## Usage

bee-hook [options]

*Now you can visit http://localhost:5000 to view your server*


## Available Options

`-p` Port to use (defaults to 5000)


## API's

### Creates new bin to post your requests

**URL** : `/api/bins`

**Method** : `POST`

**Reponses**

**Code** : `200 OK`

**Content** :

```json
{
"hash": "<hash>",
"url": "http://localhost:5000/bin/<hash>"
}
```

___

### Send your requests

**URL** : `/bin/<hash>`

**Method** : `{ POST | GET | PUT | PATCH | DELETE | OPTIONS }`

**Body** :

```json
{
"name": "Lucas Daiki",
"Age": "23"
}
```

**Reponses**

**Code** : `200 OK`

**Content** : No Content

___

### Check your requests

**URL** : `/api/bins/<hash>`

**Method** : `GET`

**Reponses**

**Code** : `200 OK`

**Content** :

```javascript
{
"created_at": "2018-02-19T21:56:47.942Z",
"last_update": "2018-02-19T22:02:40.149Z",
"bins": [
{
"method": "POST",
"body": {
"name": "Lucas Daiki",
"Age": "23"
},
"query": {},
"headers": { ... },
"created_at": "2018-02-19T22:01:17.784Z"
},
{
"method": "GET",
"body": { },
"query": { "test": "123" }, // ?test=123
"headers": { ... },
"created_at": "2018-02-19T22:02:40.149Z"
}
],
"total": 2
}
```

___

### List all bins

**URL** : `/api/bins`

**Method** : `GET`

**Reponses**

**Code** : `200 OK`

**Content** :

```javascript
[
{
"hash":"grqjbf8bti",
"created_at":"2018-02-21T23:24:05.488Z",
"last_update":"2018-02-21T23:26:00.595Z",
"total":50
},
{
"hash":"hhyasd12x",
"created_at":"2018-02-21T23:24:05.488Z",
"last_update":"2018-02-23T23:26:00.595Z",
"total":6
}
]
```

___


### Delete a hash

**URL** : `/api/bins/<hash>`

**Method** : `DELETE`

**Reponses**

**Code** : `200 OK`

**Content** : No Content
___

## Deployment (Heroku)

- Clone the `bee-hook` repository

- Install heroku [See more](https://devcenter.heroku.com/articles/getting-started-with-nodejs#set-up)

- Login Heroku
```
heroku login
```

- Create an app on Heroku
```
heroku create
```

- Deploy your hook server :D
```
git push heroku master
```

- Open the new url
```
heroku open
```

93 changes: 93 additions & 0 deletions bin/bee-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env node

const express = require('express');
const bodyParser = require('body-parser');
const argv = require('optimist')
.boolean('cors')
.argv;

require('dotenv').config()

const cors = require('cors');

const BinRepository = require('../src/BinRepository');

const app = express();

const PORT = argv.p || process.env.PORT || 5000;
const HOST = process.env.HOST || `http://localhost:${PORT}`;

const URL = `${HOST}/bin`;

const binRepository = new BinRepository();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors());

app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});

app.get('/api/bins', (req, res) => {
return binRepository.getAll()
.then((result) => {
res.status(200);
res.send(result);
})
.catch(error => {
res.status(404);
res.send(error);
});
});

app.post('/api/bins', (req, res) => {
return binRepository.generateHash()
.then((hash) => {
res.send({
hash,
url: `${URL}/${hash}`
});
res.status(200);
})
.catch(() => {
res.send('Error on create bin. Please try again');
res.status(400);
})
});

app.get('/api/bins/:hash', (req, res) => {
return binRepository.getByHash(req.params.hash)
.then((result) => {
res.status(200);
res.send(result);
})
.catch(error => {
res.status(404);
res.send(error);
});
});

app.delete('/api/bins/:hash', (req, res) => {
return binRepository.deleteHash(req.params.hash)
.then(() => {
res.status(200);
res.end();
})
.catch(error => {
res.status(404);
res.send(error);
});
});

app.all('/bin/:hash', (req, res) => {
return binRepository.create(req.params.hash, req)
.then(() => {
res.status(200);
res.end();
})
.catch(error => {
res.status(404);
res.send(error);
});
});
Loading

0 comments on commit b901c71

Please sign in to comment.