Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

appa

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

appa - npm Package Compare versions

Comparing version 4.1.0 to 5.0.0

84

index.js
var qs = require('qs')
var url = require('url')
var parse = require('body/json')
var response = require('response')
var createRouter = require('match-routes')
var createRouter = require('wayfarer')
var isType = require('type-is')
var logger = require('pino')
var httplogger = require('pino-http')
var xtend = require('xtend')

@@ -14,6 +18,17 @@ /**

options = options || {}
var router = createRouter()
options.log = options.log || { level: 'info' }
var router = app.router = createRouter('/404')
var log = logger(options.log, options.log.stream)
var httplog = httplogger(options.log, options.log.stream)
// provide a 404 fallback
on('/404', (options.notFound || notFound))
function notFound (req, res) { error(res, 'Not found') }
// ignore favicon.ico requests
on('/favicon.ico', function (req, res) { send(res, 200) })
/**
* The request, response handler that is passed to `http.createServer`, and that
* The request, response handler that is passed to `http.createServer`, and the object that
* provides methods for your app.

@@ -25,4 +40,6 @@ * @name app

function app (req, res) {
if (router.match(req, res)) return
else error(res, 404, 'Not Found')
httplog(req, res)
var parsed = url.parse(req.url)
parsed.query = qs.parse(parsed.query)
return router(parsed.pathname, req, res, parsed)
}

@@ -32,3 +49,3 @@

* Route handler
* @name on
* @name app.on
* @param {String} pathname – the route for this handler

@@ -38,12 +55,10 @@ * @param {Function} callback – the route handler

function on (pathname, callback) {
return router.on(pathname, function (req, res, context) {
context.query = qs.parse(context.query)
return router.on(pathname, function (params, req, res, ctx) {
ctx.params = params
log.info(ctx)
function handleParse (err, body) {
if (err) {
return error(res, 400, 'Bad Request, invalid JSON')
}
context.body = body
callback(req, res, context)
if (err) return error(res, 400, 'Bad Request, invalid JSON')
ctx.body = body
callback(req, res, ctx)
}

@@ -53,6 +68,6 @@

if (isType(req, ['json'])) return parse(req, res, handleParse)
return callback(req, res, context)
return callback(req, res, ctx)
}
callback(req, res, context)
callback(req, res, ctx)
})

@@ -62,4 +77,4 @@ }

/**
* Send a JSON response
* @name send
* Send a JSON object as a response
* @name app.send
* @param {Object} res – the http response object

@@ -80,8 +95,11 @@ * @param {Number} statusCode – the status code of the response, default is 200

* Send a JSON error response
* @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
*/
function error (res, statusCode, message) {
if (typeof statusCode === 'object') {
function error (res, statusCode, message, data) {
if (typeof statusCode === 'string') {
data = message
message = statusCode

@@ -91,12 +109,30 @@ statusCode = 404

return send(res, statusCode, { statusCode: statusCode, message: message })
data = data || {}
data = xtend(data, { statusCode: statusCode, message: message })
log.error(data, message)
return send(res, statusCode, data)
}
/**
* Create logs using the pino module: https://npmjs.com/pino
* @name app.log
*/
app.log = log
/**
* Parse or stringify a JSON stream using the JSONStream module: https://npmjs.com/JSONStream
* @name app.json
*/
app.json = require('JSONStream')
/**
* Compose a stream using the pump module: https://npmjs.com/pump
* @name app.pipe
*/
app.pipe = require('pump')
app.on = on
app.send = send
app.error = error
app.router = router
app.json = require('JSONStream')
app.pipe = require('pump')
return app
}
{
"name": "appa",
"version": "4.1.0",
"version": "5.0.0",
"description": "Quickly create simple JSON API services.",
"main": "index.js",
"scripts": {
"test": "standard && tape test.js | tap-spec",
"build:docs": "documentation readme -s API"
"lint": "standard",
"deps": "dependency-check . && dependency-check . --unused --no-dev",
"test:node": "tape test.js | tap-spec",
"test": "npm run lint && npm run deps && npm run test:node",
"build:api": "documentation readme -s API",
"build": "npm run build:api"
},

@@ -22,9 +26,11 @@ "repository": {

"JSONStream": "^1.1.2",
"accepts": "^1.3.2",
"body": "^5.1.0",
"match-routes": "^2.0.0",
"pino": "^2.7.5",
"pino-http": "^1.0.8",
"pump": "^1.0.1",
"qs": "^6.1.0",
"response": "^0.18.0",
"type-is": "^1.6.12"
"type-is": "^1.6.12",
"wayfarer": "^6.1.5",
"xtend": "^4.0.1"
},

@@ -31,0 +37,0 @@ "devDependencies": {

# appa
Quickly build micro services.
Quickly create simple JSON API services.

@@ -26,3 +26,5 @@ [![Travis](https://img.shields.io/travis/sethvincent/appa.svg)](https://travis-ci.org/sethvincent/appa)

http.createServer(app).listen(3000)
http.createServer(app).listen(3000, function () {
app.log.info('server started at http://127.0.0.1:3000')
})
```

@@ -39,7 +41,6 @@

- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `options.log` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** – whether appa should log using bole. default: true
### app
The request, response handler that is passed to `http.createServer`, and that
The request, response handler that is passed to `http.createServer`, and the object that
provides methods for your app.

@@ -52,3 +53,3 @@

### on
### app.on

@@ -62,5 +63,5 @@ Route handler

### send
### app.send
Send a JSON response
Send a JSON object as a response

@@ -73,3 +74,3 @@ **Parameters**

### error
### app.error

@@ -81,8 +82,20 @@ Send a JSON error response

- `response` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** – the http response object
- `res`
- `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
### app.log
Create logs using the pino module: <https://npmjs.com/pino>
### app.json
Parse or stringify a JSON stream using the JSONStream module: <https://npmjs.com/JSONStream>
### app.pipe
Compose a stream using the pump module: <https://npmjs.com/pump>
## License
[MIT](LICENSE.md)

@@ -13,3 +13,3 @@ var test = require('tape')

test('create a server', function (t) {
var app = createApp({ log: false })
var app = createApp({ log: { level: 'silent' } })
var server = createServer(app).listen(0, function () {

@@ -24,3 +24,3 @@ t.ok(app)

t.plan(6)
var app = createApp({ log: false })
var app = createApp({ log: { level: 'silent' } })

@@ -46,3 +46,3 @@ app.on('/', function (req, res, context) {

t.plan(4)
var app = createApp({ log: false })
var app = createApp({ log: { level: 'silent' } })

@@ -66,3 +66,3 @@ app.on('/', function (req, res, context) {

t.plan(5)
var app = createApp({ log: false })
var app = createApp({ log: { level: 'silent' } })

@@ -86,1 +86,23 @@ app.on('/', function (req, res, context) {

})
test('receive params', function (t) {
t.plan(7)
var app = createApp({ log: { level: 'silent' } })
app.on('/list/:listkey/item/:itemkey', function (req, res, ctx) {
t.ok(ctx.params)
t.ok(ctx.params.listkey)
t.ok(ctx.params.itemkey)
app.send(res, ctx.params)
})
var server = createServer(app).listen(3131, function () {
request({ url: 'http://127.0.0.1:3131/list/hello/item/wat', json: true }, function (err, res, body) {
t.notOk(err)
t.ok(body)
t.equal(body.listkey, 'hello')
t.equal(body.itemkey, 'wat')
server.close()
})
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc