express-wr

Wrapped Response for express framework

Usage no npm install needed!

<script type="module">
  import expressWr from 'https://cdn.skypack.dev/express-wr';
</script>

README

express-wr

Wrapped Response for express framework

Installation

npm install express-wr

Example code

Module that sends a wrapped response on Express framework based on REST Best practices (page 21)

var express = require('express'),
    http = require('http'),
    wr = require('./../')(express),
    router = require('./router'),
    app = express(),
    server;

//success response    
app.get('/200', function(req, res) {
  express.wr(res, {
    code : 200,
    data : 'example!',
    message : ''
  });
});

API

#wr(res, spec)

Method that sends a wrapped response to client.

Arguments

res This is an instance of express.Response

spec *optinalThis is an optional object that will be sent as the body of the response

  { "code" : 200,
    "data" : "http://www.example.com/resource/id",
    "message" : ""
  }

Sending different types of variables

You can also wrap different types of variables, for instace:

var express = require('express'),
    http = require('http'),
    wr = require('./../')(express),
    router = require('./router'),
    app = express(),
    server;

app.get('/string', function(req, res) {
  express.wr(res, 'sending a string!');
});

app.get('/number', function(req, res) {
  express.wr(res, 150.49);
});

app.get('/boolean', function(req, res) {
  express.wr(res, true);
});

app.get('/null', function(req, res) {
  express.wr(res, null);
});

app.get('/undefined', function(req, res) {
  express.wr(res, undefined);
});

app.get('/date', function(req, res) {
  express.wr(res, new Date());
});

app.get('/array', function(req, res) {
  express.wr(res, ['1',2,3]);
});

app.get('/empty', function(req, res) {
  express.wr(res);
});

Example response on client

If you do:

app.get('/array', function(req, res) {
  express.wr(res, ['1',2,3]);
});

You will get a 200 response with a body like this:

```json { "code" : 200, "status" : "success", "data" : ["1", 2, 3], "message" : "" } ```

If you do:

app.get('/array', function(req, res) {
  express.wr(res, {
    code : 500,
    data : 'Unable to process request',
    message : 'Invalid arguments'
  });
});

You will get a 500 response with a body like this:

```json { "code" : 500, "status" : "fail", "data" : "Unable to process request", "message" : "Invalid arguments" } ```

Sending custom properties in wrapped response

You can also send different custom properties in a wrapped response, for instace:

app.get('/custom', function(req, res) {
  express.wr(res, {
    code: 200,
    data: {},
    message: ''
  }, {
    error: 'this is an example',
    num: 123,
    obj: {}
  });
});

You will get a 200 response with a body like this:

```json { "code" : 200, "status" : "success", "data" : {}, "message" : "", "error": "this is an example", "num": 123, "obj": {} } ```

Test

npm test

License

The MIT License (MIT)

Copyright (c) 2015 Luis Alonso Valdovinos Valencia

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.