-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f3e9b9
commit 0077874
Showing
11 changed files
with
266 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"curly": true, | ||
"eqeqeq": true, | ||
"immed": true, | ||
"latedef": "nofunc", | ||
"newcap": true, | ||
"noarg": true, | ||
"sub": true, | ||
"undef": true, | ||
"unused": true, | ||
"boss": true, | ||
"eqnull": true, | ||
"node": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2015 Sergii Zinkevych | ||
|
||
Permission is hereby granted, free of charge, to any person | ||
obtaining a copy of this software and associated documentation | ||
files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, | ||
copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following | ||
conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,86 @@ | ||
# express-custom-reponse | ||
Create custom reponse method for express.js | ||
# express-custom-response | ||
|
||
Create custom response methods for express.js | ||
|
||
## Getting Started | ||
Install the module with: `npm install express-custom-response` | ||
|
||
##Quick Start | ||
```javascript | ||
require('express-custom-response')(); | ||
``` | ||
|
||
|
||
## Documentation | ||
Module contain pre-defined custom responses: | ||
|
||
**res.serverError (extra)** | ||
- *mixed* **extra** | ||
- Any data that have to be returned to user | ||
|
||
> return reponse with http code 500 and body:``` | ||
error: { | ||
error_message: 'We're sorry, a server error occurred. Please wait a bit and try again', | ||
error_code: 500, | ||
error_extra: null or extra | ||
} | ||
``` | ||
**res.forbidden (fields, extra)** | ||
- *object* **fields** | ||
- Object which contain validation error, where key is field name and value is message | ||
- *mixed* **extra** | ||
- Any data that have to be returned to user | ||
> return reponse with http code 405 and body:``` | ||
error: { | ||
error_message: 'Validation failed', | ||
error_code: 405, | ||
fields: {email: 'Email has to be uniq'}, | ||
error_extra: null or extra | ||
} | ||
``` | ||
|
||
**res.notFound (name, extra)** | ||
- *string* **name** | ||
- What server can't find | ||
- *mixed* **extra** | ||
- Any data that have to be returned to user | ||
|
||
> return reponse with http code 404 and body:``` | ||
error: { | ||
error_message: 'User not found', | ||
error_code: 404, | ||
error_extra: null or extra | ||
} | ||
``` | ||
**res.ok (extra)** | ||
- *mixed* **extra** | ||
- Any data that have to be returned to user | ||
> return reponse with http code 404 and body:``` | ||
{} or extra | ||
``` | ||
|
||
|
||
## Create own response | ||
If you want to add you own responses you should pass absolute path to module constructor | ||
```javascript | ||
require('express-custom-response')(__dirname+ '/api/responses'); | ||
``` | ||
Then each script in this directory will be available as response method by filename. Example: | ||
```javascript | ||
/** | ||
* api/responses/accepted.js | ||
* | ||
* This will be available in controllers as res.accepted(message); | ||
*/ | ||
|
||
module.exports = function(redirectTo, extra){ | ||
//this is points to express.response | ||
this.status(202); | ||
this.send(message); | ||
} | ||
``` | ||
If you want to override exists pre-defined response simple add new script to your directory with name of response which you want to override |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Created by Sergii on 26.04.2015. | ||
*/ | ||
|
||
|
||
module.exports = function(message, extra){ | ||
// Set status code | ||
this.status(403); | ||
this.send({ | ||
error: { | ||
error_message: message, | ||
error_code: 403, | ||
error_extra: extra || null | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Created by Sergii on 26.04.2015. | ||
*/ | ||
|
||
|
||
module.exports = function(name, extra){ | ||
// Set status code | ||
this.status(404); | ||
this.send({ | ||
error: { | ||
error_message: name + ' not found', | ||
error_code: 404, | ||
error_extra: extra || null | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Created by Sergii on 26.04.2015. | ||
*/ | ||
|
||
|
||
module.exports = function(extra){ | ||
// Set status code | ||
this.status(200); | ||
this.send(extra || {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Created by Sergii on 26.04.2015. | ||
*/ | ||
|
||
|
||
module.exports = function(extra){ | ||
// Set status code | ||
this.status(500); | ||
this.send({ | ||
error: { | ||
error_message: 'We\'re sorry, a server error occurred. Please wait a bit and try again', | ||
error_code: 500, | ||
error_extra: extra || null | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Created by Sergii on 26.04.2015. | ||
*/ | ||
|
||
|
||
module.exports = function(fields, extra){ | ||
// Set status code | ||
this.status(405); | ||
this.send({ | ||
error: { | ||
error_message: 'Validation failed', | ||
error_code: 405, | ||
fields: fields, | ||
error_extra: extra || null | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* express-custom-response | ||
* https://github.com/fireridlle/express-custom-response | ||
* | ||
* Copyright (c) 2015 Sergii Zinkevych | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var express = require('express'), | ||
fs = require('fs') | ||
|
||
|
||
var defaultReponse = ['serverError', 'validation', 'forbidden', 'notFound', 'ok']; | ||
|
||
|
||
module.exports = function(reponse_path){ | ||
//check if directory exists | ||
if(fs.existsSync(reponse_path)){ | ||
//get user reponses | ||
var userRes = fs.readdirSync(reponse_path); | ||
|
||
//add user reponses | ||
userRes.forEach(function(response){ | ||
var reponseName = response.split('.')[0]; | ||
express.response[reponseName] = require(reponse_path + '/' + response); | ||
}); | ||
|
||
//add default reponses | ||
defaultReponse.forEach(function(reponseName){ | ||
//if user didn't override default reponse | ||
if(!express.response[reponseName]){ | ||
express.response[reponseName] = require(__dirname + '/default/' + reponseName + '.js'); | ||
} | ||
}) | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "express-custom-response", | ||
"description": "Create custom response methods for express.js", | ||
"version": "0.1.1", | ||
"homepage": "https://github.com/fireridlle/express-custom-response", | ||
"author": { | ||
"name": "Sergii Zinkevych", | ||
"email": "[email protected]" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "[email protected]:fireridlle/express-custom-response.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/fireridlle/express-custom-response/issues" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/fireridlle/express-custom-response/blob/master/LICENSE-MIT" | ||
} | ||
], | ||
"main": "lib/express-custom-response", | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
}, | ||
"keywords": [] | ||
} |