express-router-tcomb

0.1.0 • Public • Published

express-router-tcomb

Build Status

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

Dependents (0)

Package Sidebar

Install

npm i express-router-tcomb

Weekly Downloads

10

Version

0.1.0

License

MIT

Last publish

Collaborators

  • ewnd9