https://github.com/ph1p/hemera-fastify
hemera http plugin with hemera-fastify
https://github.com/ph1p/hemera-fastify
Last synced: about 1 year ago
JSON representation
hemera http plugin with hemera-fastify
- Host: GitHub
- URL: https://github.com/ph1p/hemera-fastify
- Owner: ph1p
- License: mit
- Created: 2018-03-22T22:56:13.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-22T23:04:43.000Z (over 8 years ago)
- Last Synced: 2025-03-30T06:34:19.671Z (over 1 year ago)
- Language: JavaScript
- Size: 3.91 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Hemera-fastify
[](https://www.npmjs.com/package/hemera-fastify)
Copied from [hemera-web](https://github.com/hemerajs/hemera/tree/master/packages/hemera-web) and switched from express 4 to fastify.
Also I've kicked out some dependencies.
> Under development
## Example
```javascript
const Hemera = require('nats-hemera');
const HemeraJoi = require('hemera-joi');
const hemeraWeb = require('../hemera-fastify/index');
const nats = require('nats').connect({
url: 'nats://0.0.0.0:4222'
});
const hemera = new Hemera(nats, {
logLevel: 'info'
});
const CustomError = hemera.createError('CustomError');
hemera.use(hemeraWeb, {
// set fastify config
fastify: {},
beforeStart(fastify) {
// Add own content parser
fastify.addContentTypeParser('text/html', (req, done) => {
var body = '';
req.on('data', data => {
body += data;
});
req.on('end', () => {
try {
done(null, body);
} catch (e) {
done(e);
}
});
req.on('error', done);
});
},
port: 3000,
host: '127.0.0.1',
pattern: {
topic: 'math',
cmd: 'add'
}
});
const start = async () => {
try {
// establish connection and bootstrap hemera
await hemera.ready();
hemera.add(
{
topic: 'math',
cmd: 'add'
},
(req, cb) => {
cb(null, `
${parseInt(req.a) + parseInt(req.b)}
`);
// cb(null, {
// result: parseInt(req.a) + parseInt(req.b)
// });
// const error = new CustomError()
// error.statusCode = 404
// cb(error)
}
);
hemera.log.info('service listening');
} catch (err) {
hemera.log.error(err);
process.exit(1);
}
};
start();
```