Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
koa-architect
Advanced tools
Readme
Reads a folder with middleware and routes and reduces them to middleware using koa-mount and koa-trie-router.
That eventually allows you run your app extremely simple:
const Koa = require('koa')
const architect = require('koa-architect')
let app = new Koa()
for(let middleware of architect.readMiddlewareAndRoutes('./middleware')) {
app.use(middleware)
}
All of you need is keep in mind next things:
// router.use(fn)
exports.use = function (ctx) {
}
// router.get('/', fn)
exports.get = {
'index': function (ctx) {
}
}
// router.post('/foo', fn)
// router.post('/bar/:id', fn)
exports.post = {
'foo': function (ctx) {
},
'bar/:id': function (ctx) {
}
}
Assume, we have next folder tree and code:
middleware/
middleware/01-foo/index.js
middleware/02-bar/index.js
middleware/route/index.js
middleware/nested/index.js
middleware/nested/middleware/index/index.js
middleware/nested/middleware/baz/index.js
middleware/01-foo/index.js
// Middleware
module.exports = function (ctx, next) {
// Doing here some work and go next
ctx.state.foo = 1
return next()
}
middleware/02-bar/index.js
// Middleware
module.exports = function (ctx, next) {
// Doing here some work and go next
ctx.state.bar = 1
return next()
}
middleware/route/index.js
// Routes
exports.get = {
'index': function (ctx) {
ctx.response.body = ctx.state
}
}
exports.post = {
'test/:id': function (ctx) {
ctx.response.body = ctx.params.id
}
}
middleware/nested/index.js
// Nested middleware and/or routes
const path = require('path')
const architect = require('koa-architect')
// Since this folder contains nested middleware/routes
// we need use koa-architect to read them
exports.use = architect.readMiddlewareAndRoutes(path.join(__dirname, './middleware'))
middleware/nested/middleware/index/index.js
// Routes
exports.use = function (ctx) {
ctx.response.body = ctx.originalUrl
}
middleware/nested/middleware/baz/index.js
// Routes
exports.get = {
'index': function (ctx) {
ctx.response.body = ctx.originalUrl
}
}
And we launch our app using this way:
const Koa = require('koa')
const architect = require('koa-architect')
let app = new Koa()
for(let middleware of architect.readMiddlewareAndRoutes('./middleware')) {
app.use(middleware)
}
Thus we will get a server which handle requests next way:
GET --> /
GET <-- 404 "Not Found"
GET --> /route
GET <-- 200 {"foo":1,"bar":1}
POST --> /route/test/1
POST <-- 200 "1"
GET --> /nested
GET <-- 200 "/nested"
GET --> /nested/baz
GET <-- 200 "/nested/baz"
See test/fixtures for details.
npm install koa-architect
You could find this module in npm like koa-architect.
FAQs
Automates mounting and routing
The npm package koa-architect receives a total of 74 weekly downloads. As such, koa-architect popularity was classified as not popular.
We found that koa-architect demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.