Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Many of us use node.js with express in our job or own projects. But now we can use such interesting features like async/await. We can use koa or make error handler wrapper for express. But itâs not cool.
Express and koa use middleware in kernel as the main pattern. Itâs copied from Ruby framework sinatra and itâs not asynchronous. You can compare it with loopââânot declarative and non asynchronous.
If you look how node.js server works you can see single process with ârequestâ events.
const http = require('http');const server = http.createServer( (req, res) => { ctx.res.statusCode = 200; res.end('okay');});
And with promises:
const http = require('http');const server = http.createServer( (req, res) => { Promise.resolve({ req, res }) .then(ctx => { ctx.res.statusCode = 200; ctx.res.end('okay'); return ctx; });});
So if you use loop or middleware or any non asynchronous code all other request will be waiting. The main idea of node.js and javascript is to use non blocking actions. Thatâs why we have callbacks, promises and async/await.
So after researching I decided to create a new framework. Letâs meet YEPS.
npm i -S yeps
app.js
const App = require('yeps');const app = module.exports = new App();app.then(async ctx => { ctx.res.statusCode = 200; ctx.res.end('Ok');});app.catch(async (err, ctx) => { ctx.res.statusCode = 500; ctx.res.end(err.message);});
bin/www
#!/usr/bin/env nodeconst http = require('http');const app = require('../app');http .createServer(app.resolve()) .listen(parseInt(process.env.PORT || '3000', 10));
package.json
"scripts": { "start": "node bin/www"}
First of all itâs 100% promise based frameworkâââno blocking actions in kernel.
Promise like middleware
app.then(async ctx => { ctx.res.end('test'); return app.reject(); // the same as Promise.reject()}).then(async () => { // it wont work}).catch(async () => { // it wont work});
You can check it by yourself just look benchmark results. I tested YEPS with koa and express.
YEPS gives possibility to execute modules in parallel mode using Promise.all().
const App = require('yeps');const app = new App();const error = require('yeps-error');const logger = require('yeps-logger');app.all([ logger(), error()]);app.then(async ctx => { ctx.res.writeHead(200, {'Content-Type': 'text/plain'}); ctx.res.end('Ok');});app.catch(async (err, ctx) => { ctx.res.writeHead(500); ctx.res.end(err.message);});
Or
app.all([ logger(), error()]).then(async ctx => { ctx.res.writeHead(200, {'Content-Type': 'text/plain'}); ctx.res.end('Ok');}).catch(async (err, ctx) => { ctx.res.writeHead(500); ctx.res.end(err.message);});
Itâs useful if you need to make connection to DB and make some other stuff like registration logging system, error handlerâŠ
The same idea we can use for routing. You donât need to think which router is the most popular and put it higher like for express. We can start checking all routers at the same time and reject checking if we found it.
const Router = require('yeps-router');const router = new Router();router.catch({ method: 'GET', url: '/' }).then(async ctx => { ctx.res.writeHead(200); ctx.res.end('homepage'); });router.get('/test').then(async ctx => { ctx.res.writeHead(200); ctx.res.end('test'); }).post('/test/:id').then(async ctx => { ctx.res.writeHead(200); ctx.res.end(ctx.request.params.id);});app.then(router.resolve());
And last but not least if you already have some express middlewares you can use express wrapper and itâs easy to migrate just look into YEPSÂ modules.
And some more examples:
const App = require('yeps');const server = require('yeps-server');const app = new App();const error = require('yeps-error');const Router = require('yeps-router');const router = new Router();const wrapper = require('yeps-express-wrapper');const path = require('path');
// express middlewareconst bodyParser = require('body-parser');const favicon = require('serve-favicon');app.then(wrapper(favicon(path.join(__dirname, 'public', 'favicon.ico'))));
app.all([ error(), wrapper(bodyParser.json()),]);router.get('/').then(async ctx => { console.log(ctx.req.body); ctx.res.writeHead(200); ctx.res.end('test');});app.then(router.resolve());server.createHttpServer(app);
Letâs get fun with node.js.
Yet Another Event Promised Server was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.