https://github.com/mauris/madtom
Minimalistic JSON over TCP/TLS server framework (Express.js without the HTTP)
https://github.com/mauris/madtom
api framework server tcp tls web
Last synced: about 1 month ago
JSON representation
Minimalistic JSON over TCP/TLS server framework (Express.js without the HTTP)
- Host: GitHub
- URL: https://github.com/mauris/madtom
- Owner: mauris
- Created: 2019-05-05T09:43:17.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-05-05T09:43:25.000Z (almost 6 years ago)
- Last Synced: 2025-01-30T06:43:48.131Z (3 months ago)
- Topics: api, framework, server, tcp, tls, web
- Language: JavaScript
- Homepage:
- Size: 3.91 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Madtom JSON over TCP Framework
Inspired by the [json-over-tcp](https://github.com/turn/json-over-tcp) project and [Express.js Web Framework](https://expressjs.com/), Madtom is a TCP server framework that can process requests and responses in a middleware fashion similar to Express.js.
Optionally, Madtom also supports TCP with TLS out of the box.
````javascript
const madtom = require('madtom');const app = madtom({
/* Enable server-side TLS identity */
tls: {
key: './server-key.pem',
cert: './server-cert.pem',
},
});/* use JSON as the parser for request body */
app.use(madtom.parsers.json);app.tryCatch((err, req, res, next) => {
console.log(`Error occurred:\n\t${err.message}`);
});/*
Add a router to the
*/
const router = madtom.Router();
app.use(router);/*
Executes only when request document has
the key "fruit" in it.
*/
router
.filter(doc => doc.fruit)
.execute((req, res, next) => {
if (req.body.fruit === 'tomato') {
// handle the error
next(new Error('I think tomato is considered vegetable'));
return;
}
res.json({ status: 'ok' });
});/*
Executes only when request document is an object
and has the key "consume".emitKeyValue() will transform the request document into an
array of key-value.
*/
router
.isObject()
.hasKey('consume')
.emitKeyValue()
.execute((req, res, next) => {
req.body.forEach((tuple) => {
console.log(`${tuple.key}: ${tuple.value}`);
});
res.json({ status: 'ok' });
});/*
Start listening
*/
app.listen(8001, 'madtom.test', () => {
console.log('listening!');
});
````Source code open sourced under the MIT license.