express-router-tcomb
v0.1.0
Published
Router with a query / body / response (for tests) validation via tcomb
Downloads
13
Readme
express-router-tcomb
Router with a query
/ body
/ response
(for tests) validation via tcomb
Install
$ npm install --save express-router-tcomb
Usage
'use strict';
const t = require('tcomb');
const express = require('express');
const Router = require('express-router-tcomb');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: false, limit: '50mb' }));
app.use(bodyParser.json({ limit: '50mb' }));
const router = Router();
const File = t.struct({
name: t.String,
createdAt: t.String,
updatedAt: t.String
});
const files = [{ name: 'fileA', createdAt: '2014', updatedAt: '2014' }];
router.get({
path: '/api/v1/files',
schema: {
query: t.struct({
name: t.String
}),
response: t.struct({
files: t.list(File)
})
},
handler: (req, res) => {
res.json({ files: files.filter(file => file.name === req.query.name) });
}
});
router.post({
path: '/api/v1/files',
schema: {
body: t.struct({ file: File }),
response: t.struct({ file: File })
},
handler: (req, res) => {
const { file } = req.body;
files.push(file);
res.json({ file });
}
});
app.use('/', router.getRoutes());
app.use((err, req, res, next) => {
if (!err) {
return next();
}
console.log(err.stack || err);
res.status(err.status || 500).json({ status: 'error' });
});
app.listen(3000);
License
MIT © ewnd9