micro express / koa like with async middleware support.
prototype for node in-memory session.
npm install 'git+https://github.com/tpoisseau/uNodeHttpServer.git#1.2.0'
index.js
import App, {parseCookie, sessionInMemory} from 'u-http-server';
const app = new App();
app
.use(ctx => console.log(ctx.request.url))
.use(parseCookie)
.use(sessionInMemory)
.get('/:category/:page', ({params, session, cookies}) => ({params, session, cookies}))
.get('/:test', ({params, session, cookies}) => ({params, session, cookies}))
.route('/', ctx => 'Hello World !');
app.init({protocol: 'https'})
.then(app => app.applyOnClientError())
.then(app => app.listen(3000))
.then(info => console.log('server listening on', info))
.catch(console.error);
- node 12 is required. App class use private field
- For supporting https ans http2, added a
async init(options)
method inApp
class. Async is here for let you autogenerate self-signed certificate (via nodechild_process.exec
foropenssl
) if you want useautogenerate self-signed certificate
abilities,openssl
should be installed and accessible in$PATH
listen(port)
is nowasync listen(port)
resolved when server is truly listening and return a string url of server
- middleware are passed in order (matching http methods and route)
- next route are passed in order (matching http methods and route)
ctx is passed to middleware as first parameters
you can populate in what ever you want,
it's a simple object with request and response in it at start.
you middleware recieve in second parameters the returns of last middleware (usefull if you want chain some of them without pollute ctx)
Please read API-Doc
or directly index.d.ts for complete api doc