Comparing version 2.0.1 to 2.0.2
@@ -1,2 +0,2 @@ | ||
module.exports.send = (req, res) => (data = 200, code = 200, headers = {}) => { | ||
module.exports.send = (req, res) => (data = 200, code = 200, headers = {}, cb = () => {}) => { | ||
res.setHeader('content-type', 'text/plain') | ||
@@ -7,38 +7,31 @@ Object.keys(headers).forEach((key) => { | ||
return new Promise(async (resolve, reject) => { | ||
if (data instanceof Promise) data = await data | ||
if (data instanceof Error) { | ||
code = data.status || data.code || 500 | ||
data = { | ||
errClass: data.constructor.name, | ||
code, | ||
message: data.message, | ||
data: data.data | ||
} | ||
} else if (typeof data === 'number') { | ||
code = parseInt(data, 10) | ||
data = res.body | ||
if (data instanceof Error) { | ||
code = data.status || data.code || 500 | ||
data = { | ||
errClass: data.constructor.name, | ||
code, | ||
message: data.message, | ||
data: data.data | ||
} | ||
} else if (typeof data === 'number') { | ||
code = parseInt(data, 10) | ||
data = res.body | ||
} | ||
// emit response event | ||
const params = { | ||
res, | ||
req, | ||
data, | ||
code | ||
} | ||
res.emit('response', params) | ||
// emit response event | ||
const params = { | ||
res, | ||
req, | ||
data, | ||
code | ||
} | ||
res.emit('response', params) | ||
if (typeof data === 'object') { | ||
res.setHeader('content-type', 'application/json') | ||
params.data = JSON.stringify(params.data) | ||
} | ||
if (typeof data === 'object') { | ||
res.setHeader('content-type', 'application/json') | ||
params.data = JSON.stringify(params.data) | ||
} | ||
res.writeHead(params.code) | ||
res.end(params.data, (err) => { | ||
if (err) reject(err) | ||
resolve() | ||
}) | ||
}) | ||
res.writeHead(params.code) | ||
res.end(params.data, cb) | ||
} |
{ | ||
"name": "restana", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Super fast and minimalist web framework for building REST micro-services.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -48,20 +48,21 @@ # REST-Ana | ||
service.get('/pets/:id', (req, res) => { | ||
res.send(PetsModel.findOne(req.params.id)); | ||
service.get('/pets/:id', async (req, res) => { | ||
res.send(await PetsModel.findOne(req.params.id)); | ||
}); | ||
service.get('/pets', (req, res) => { | ||
res.send(PetsModel.find()); | ||
service.get('/pets', async (req, res) => { | ||
res.send(await PetsModel.find()); | ||
}); | ||
service.delete('/pets/:id', (req, res) => { | ||
res.send(PetsModel.destroy(req.params.id)); | ||
service.delete('/pets/:id', async (req, res) => { | ||
res.send(await PetsModel.destroy(req.params.id)); | ||
}); | ||
service.post('/pets/:name/:age', (req, res) => { | ||
res.send(PetsModel.create(req.params)); | ||
service.post('/pets/:name/:age', async (req, res) => { | ||
res.send(await PetsModel.create(req.params)); | ||
}); | ||
service.patch('/pets/:id', function (req, res) { | ||
res.send(this.update(req.params.id, JSON.stringify(req.body))); | ||
service.patch('/pets/:id', async function (req, res) { | ||
const update = await this.update(req.params.id, JSON.stringify(req.body)) | ||
res.send(update); | ||
}, PetsModel); // attaching this context | ||
@@ -95,3 +96,5 @@ | ||
service.post('/star/:username', async (req, res) => { | ||
const stars = await starService.star(req.params.username) | ||
await starService.star(req.params.username) | ||
const stars = await starService.count(req.params.username) | ||
return stars | ||
@@ -102,2 +105,17 @@ }); | ||
### Sending custom headers: | ||
```js | ||
res.send('Hello World', 200, { | ||
'x-response-time': 100 | ||
}); | ||
``` | ||
### Acknowledge from low-level `end` operation | ||
```js | ||
res.send('Hello World', 200, {}, (err) => { | ||
if (err) { | ||
// upppsss | ||
} | ||
}); | ||
``` | ||
### Middleware usage: | ||
@@ -144,9 +162,4 @@ ```js | ||
### Sending custom headers: | ||
```js | ||
res.send('Hello World', 200, { | ||
'x-response-time': 100 | ||
}); | ||
``` | ||
Third party middlewares support: | ||
@@ -168,5 +181,4 @@ > Almost all middlewares using the *function (req, res, next)* signature format should work, considering that no custom framework feature is used. | ||
### String response ('Hello World!') | ||
* polka: Requests/sec 37911.81 | ||
* fastify: Requests/sec 36894.86 | ||
* **restana**: Requests/sec 30066.89 | ||
* **restana**: Requests/sec 35652.75 | ||
* koa: Requests/sec 23486.64 | ||
@@ -177,5 +189,6 @@ * express: Requests/sec 16057.22 | ||
* fastify: Requests/sec 33143.12 | ||
* **restana**: Requests/sec 28083.14 | ||
* **restana**: Requests/sec 32315.78 | ||
* koa: Requests/sec 22485.43 | ||
* express: Requests/sec 14569.78 | ||
* polka: N/A - JSON response auto-detection no supported! | ||
> [Polka](https://github.com/lukeed/polka) micro-framework is not considered because it misses JSON response auto-detection. When content is String, `polka` performance is best. |
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
11759
188
159