1.0.1 • Published 5 years ago

polkadot-middleware v1.0.1

Weekly downloads
697
License
WTFPL
Repository
github
Last release
5 years ago
npm i polkadot-middleware

So you're using the awesome polkadot server, but you're missing that sweet middleware action – it's a pain to have to compose all those functions yourself, isn't it?

polkadot(
	handleErrors(
		setCacheControl(
			(req, res) => 'Sup dawg'
		)
	)
).listen(8080)

function handleErrors(next) {
	return async(req, res) => {
		try {
			return await next(req, res)
		} catch (err) {
			res.statusCode = 500

			return err.message || err
		}
	}
}

function setCacheControl(next) {
	return async(req, res) => {
		res.setHeader(`Cache-Control`, `public, max-age=` + 3600)
		return next(req, res)
	}
}

Well, until TC39 figures out the pipeline operator, you can use this library to give yourself that sweet linear middleware experience.

const polkadot = require('polkadot')
const middleware = require('polkadot-middleware')

middleware(
	polkadot,
	handleErrors,
	setCacheControl,
	(req, res) => 'Sup dawg'
).listen(8080)

License

WTFPL