Comparing version 5.0.1 to 6.0.0
@@ -0,1 +1,3 @@ | ||
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
# createApp | ||
@@ -46,6 +48,15 @@ | ||
- `res` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** – the http response object | ||
- `statusCode` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** – the status code of the response, default is 200 | ||
- `data` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** – the data that will be stringified into JSON | ||
**Examples** | ||
```javascript | ||
var send = require('appa/send') | ||
app.on('/', function (req, res, ctx) { | ||
send({ message: 'hi' }).pipe(res) | ||
}) | ||
``` | ||
# app.error | ||
@@ -57,5 +68,14 @@ | ||
- `response` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** – the http response object | ||
- `statusCode` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** – the status code of the response, default is 404 | ||
- `message` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** – the message that will be stringified into JSON | ||
- `data` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** – additional data about the error to send in the response | ||
**Examples** | ||
```javascript | ||
var error = require('appa/error') | ||
app.on('/', function (req, res, ctx) { | ||
error(404, 'Resource not found').pipe(res) | ||
}) | ||
``` |
var xtend = require('xtend') | ||
var send = require('./send') | ||
module.exports = function error (res, statusCode, message, data) { | ||
module.exports = function error (statusCode, message, data) { | ||
if (typeof statusCode === 'string') { | ||
@@ -13,3 +13,3 @@ data = message | ||
data = xtend(data, { statusCode: statusCode, message: message }) | ||
return send(res, statusCode, data) | ||
return send(statusCode, data) | ||
} |
var http = require('http') | ||
var app = require('./index')() | ||
var app = require('../index')() | ||
var send = require('../send') | ||
var log = app.log | ||
app.on('/', function (req, res, context) { | ||
app.send(res, { message: 'oh hey friends' }) | ||
send(200, { message: 'oh hey friends' }).pipe(res) | ||
}) | ||
http.createServer(app).listen(3000, function () { | ||
app.log.info('server started at http://127.0.0.1:3000') | ||
log.info('server started at http://127.0.0.1:3000') | ||
}) |
20
index.js
@@ -29,6 +29,6 @@ var assert = require('assert') | ||
on('/404', (options.notFound || notFound)) | ||
function notFound (req, res) { error(res, 'Not found') } | ||
function notFound (req, res) { error('Not found').pipe(res) } | ||
// ignore favicon.ico requests | ||
on('/favicon.ico', function (req, res) { send(res, 200) }) | ||
on('/favicon.ico', function (req, res) { send(200).pipe(res) }) | ||
@@ -64,3 +64,3 @@ /** | ||
function handleParse (err, body) { | ||
if (err) return error(res, 400, 'Bad Request, invalid JSON') | ||
if (err) return error(400, 'Bad Request, invalid JSON').pipe(res) | ||
ctx.body = body | ||
@@ -100,5 +100,10 @@ callback(req, res, ctx) | ||
* @name app.send | ||
* @param {Object} res – the http response object | ||
* @param {Number} statusCode – the status code of the response, default is 200 | ||
* @param {Object} data – the data that will be stringified into JSON | ||
* @example | ||
* var send = require('appa/send') | ||
* | ||
* app.on('/', function (req, res, ctx) { | ||
* send({ message: 'hi' }).pipe(res) | ||
* }) | ||
*/ | ||
@@ -110,6 +115,11 @@ app.send = send | ||
* @name app.error | ||
* @param {Object} response – the http response object | ||
* @param {Number} statusCode – the status code of the response, default is 404 | ||
* @param {String} message – the message that will be stringified into JSON | ||
* @param {Object} data – additional data about the error to send in the response | ||
* @example | ||
* var error = require('appa/error') | ||
* | ||
* app.on('/', function (req, res, ctx) { | ||
* error(404, 'Resource not found').pipe(res) | ||
* }) | ||
*/ | ||
@@ -116,0 +126,0 @@ app.error = error |
{ | ||
"name": "appa", | ||
"version": "5.0.1", | ||
"version": "6.0.0", | ||
"description": "Quickly create simple JSON API services.", | ||
@@ -9,3 +9,3 @@ "main": "index.js", | ||
"deps": "dependency-check . && dependency-check . --unused --no-dev", | ||
"test:node": "tape test.js | tap-spec", | ||
"test:node": "tape tests/index.js | tap-spec", | ||
"test": "npm run lint && npm run deps && npm run test:node", | ||
@@ -12,0 +12,0 @@ "docs:api": "documentation build index.js -f md -o docs/API.md", |
@@ -42,3 +42,3 @@ # appa | ||
app.on('/', function (req, res, context) { | ||
send(res, { message: 'oh hey friends' }) | ||
send({ message: 'oh hey friends' }).pipe(res) | ||
}) | ||
@@ -45,0 +45,0 @@ |
var response = require('response') | ||
module.exports = function send (res, statusCode, data) { | ||
module.exports = function send (statusCode, data) { | ||
if (typeof statusCode === 'object') { | ||
@@ -9,3 +9,3 @@ data = statusCode | ||
return response.json(data).status(statusCode).pipe(res) | ||
return response.json(data).status(statusCode) | ||
} |
@@ -6,3 +6,4 @@ var test = require('tape') | ||
var createApp = require('./index') | ||
var createApp = require('../index') | ||
var send = require('../send') | ||
@@ -30,3 +31,3 @@ function createServer (app) { | ||
t.ok(context) | ||
app.send(res, { hello: 'hi' }) | ||
send({ hello: 'hi' }).pipe(res) | ||
}) | ||
@@ -49,3 +50,3 @@ | ||
app.on('/', function (req, res, context) { | ||
app.send(res, context.query) | ||
send(context.query).pipe(res) | ||
}) | ||
@@ -94,3 +95,3 @@ | ||
t.ok(ctx.params.itemkey) | ||
app.send(res, ctx.params) | ||
send(ctx.params).pipe(res) | ||
}) | ||
@@ -97,0 +98,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2
53739
21
232