Dumpling in japaneese.
POC for functional controllers
and route-handlers
in express.
- Functional way of writing routers
- Follow function programming concepts
- Composition over inheritance
I'm not very familiar with functional programming
and it's concepts. I'm learning it now and it turns out the v0.0.2
dango code was not functional at all and it has been scraped see tag v0.0.2. I'll be proposing a new API
for dango again. As we have been doing OOP
for so long, FP
will take some time to be fully understood.
v2.0.0
- The router handler ctx is now an object
// B= body, P= params, Q=query
// types createRoute<B,P,Q>
ctx = { req, res, body, params, query }
- see
createRoute
for usage
v1.0.0
CreateHandler
has been scrapedCreateController
params changed.
Core:
- A wrapper on
express
to inject controllers. - Can be used as
import express from 'express';
import { createExpressServer } from 'dango-core';
const app = express();
createExpressServer(app, { controllers: ['./controllers/*.ts'] }).listen(3000, () => {
console.log('http://localhost:3000');
});
- controller definition should be a
glob
path.
- A controller can be created with
createController
method.
import { createController } from 'dango-core';
export default createController(
'/',
[
{
path: '/',
method: 'get',
handler: ({req, res, params, query}) => {
res.send('is this working ?');
},
middlewares: [
(req, res, next) => {
res.send("I'm local");
},
],
},
],
[
(req, res, next) => {
res.send("I'm global");
},
],
);
- Should be default exported from the controller file.
This is a low level API which can be used to create routes with proper type definitions for request body,params and query. Can be used with createController
for modularity.
-
BREAKING CHANGE
v2.0.0
the handler ctx is now an object
-
standalone
import { createRoute } from 'dango-core';
const userRoute = createRoute<{ user: string }>({
path: '/user',
method: 'options',
handler: ({req, res, body}) => {
// proper ts definitions
req.body.user;
},
});
- with controller
import { createController } from 'dango-core';
export default createController('/aa', [userRoute]);
- With express router style
function chaining
import { createRoute } from 'dango-core';
const home = createRoute<{ user: string }>('/')
.method('get')
.handler(({ res }) => res.send('OK'));
import express from 'express';
import { createExpressServer } from 'dango-core';
const app = express();
createExpressServer(app, {
controllers: ['controllers/**.ts'],
middlewares: [
(req, res, next) => {
res.send("I'm global");
},
],
}).listen(4000, () => console.log('http://localhost:4000'));
import { createController } from 'dango-core';
export default createController(
'/test',
[
{
path: '/yolo',
method: 'get',
handler: ({req, res}) => {
res.send('is this working ?');
},
},
],
[
(req, res, next) => {
res.send("I'm controller specific");
},
],
);
import { createController } from 'dango-core';
export default createController('/test', [
{
path: '/yolo',
method: 'get',
handler: ({req, res}) => {
res.send('is this working ?');
},
middlewares: [
(req, res, next) => {
res.send("I'm route specific");
},
],
},
]);
-
- send HTTP errors from response with proper intellisense
import { createController, createRoute } from 'dango-core'; const someRoute = createRoute<{ user: string }>({ path: '/', method: 'get', handler: ({req, res, body}) => { res.sendError(401,"Nope") }, }); export default createController('/home', [someRoute]);
- send HTTP errors from response with proper intellisense
Package | version |
---|---|
Core | |
Example | NA |