Dumpling in japaneese.
POC for functional controllers and route-handlers in express.
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
// B= body, P= params, Q=query
// types createRoute<B,P,Q>
ctx = { req, res, body, params, query }
createRoute for usagev1.0.0
CreateHandler has been scrapedCreateController params changed.
Core:
express to inject controllers.import express from 'express';
import { createExpressServer } from 'dango-core';
const app = express();
createExpressServer(app, { controllers: ['./controllers/*.ts'] }).listen(3000, () => {
console.log('http://localhost:3000');
});
glob path.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");
},
],
);
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 objectstandalone ```ts 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
```ts
import { createController } from 'dango-core';
export default createController('/aa', [userRoute]);
function chaining
```ts
import { createRoute } from 'dango-core';const home = createRoute<{ user: string }>('/') .method('get') .handler(({ res }) => res.send('OK'));
#### Middlewares
##### Global
```ts
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");
},
],
},
]);
response.sendErrorimport { 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]);
| Package | version |
|---|---|
| Core | |
| Example | NA |
Generated using TypeDoc