An imitation of golang web framework echo
npm i echoey -S
import { Echo, Context, handlerFunc } from 'echoey';
import { timer } from 'echoey/middleware';
const e = new Echo();
e.Use(timer);
e.Use(testMid);
e.GET('/hello', (c: Context) => {
c.String(200, 'hello echoey');
});
e.Static('/static', 'assets');
e.Start(3000);
function testMid(next: handlerFunc) {
return async (c: Context) => {
console.log('testMid');
await next(c);
};
}
const { Echo } = require('echoey');
const { timer } = require('echoey/middleware');
const e = new Echo();
e.Use(timer);
e.Use(testMid);
e.GET('/hello', c => {
c.String(200, 'hello echoey');
});
e.GET('/sleep', async c => {
await sleep();
c.String(200, 'sleep after 1s');
});
e.Start(3000);
function testMid(next) {
return async c => {
console.log('testMid');
await next(c);
};
}
function sleep(delay = 1000) {
return new Promise(resolve => setTimeout(resolve, delay));
}
const e = new Echo();
e.GET('/hello', (c: Context) => {
c.String(200, 'hello test');
});
e.Use('/hello', testMid);
let g = e.Group('/admin', testMid);
g.GET('/test', (c: Context) => {
c.String(200, 'asd');
});
e.Start(3000);
function testMid(next: handlerFunc) {
return async (c: Context) => {
console.log('testMid');
await next(c);
};
}
const e = new Echo();
e.GET(
'/test',
async (c: Context) => {
c.JSON(200, { a: 1 });
},
testMid,
);
e.AddGroup(UserGroup);
e.Start(3000);
function UserGroup(e: Echo) {
const g = new Group('/user', e);
g.GET('/test', (c: Context) => {
c.String(200, 'user hello');
});
return g;
}
function testMid(next: handlerFunc): handlerFunc {
return async (c: Context) => {
console.log('testMid1');
await next(c);
};
}
https://github.com/cooperhsiung/echoey/tree/master/src/example
middlewares
import { timer, cors, compress, serveStatic } from 'echoey/middleware';
Since handler and middlewares are chained to execute, unpromised middleware might break the promises chain.
:smiley: It is commended to write middleware in this way
function testMid(next: handlerFunc) {
return async (c: Context) => {
console.log('testMid');
await next(c);
};
}
:confused: not commended
function testMid(next: handlerFunc) {
return (c: Context) => {
console.log('testMid');
next(c);
};
}
Author: cooperhsiung
Source Code: https://github.com/cooperhsiung/echoey
License: MIT