Nestable Processes Mutate
Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

yeps

1.0.1 • Public • Published

Yet Another Event Promised Server

Simple promised node http request-response handler

NPM

npm version Build Status Coverage Status Linux Build Windows Build

Dependency Status devDependency Status NSP Status

Known Vulnerabilities License GitHub stars GitHub forks GitHub issues Twitter

Node http server is simple and fast. But it works with callbacks:

const http = require('http');

const server = http.createServer( (req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('okay');
});

If we want to use promise (and async await):

const http = require('http');
    
const server = http.createServer( (req, res) => {
  Promise.resolve({ req, res })
    .then(ctx => {
      
        ctx.res.writeHead(200, {'Content-Type': 'text/plain'});
        ctx.res.end('okay');
      
        return ctx;
    });
});

And the same with 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.setHeader('Content-Type', 'application/json');
  ctx.res.end('{"status":"OK"}');
});

app.catch(async (err, ctx) => {
  ctx.res.statusCode = 500;
  ctx.res.setHeader('Content-Type', 'application/json');
  ctx.res.end('{"error":true}');
});

bin/www

#!/usr/bin/env node

const 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"
}

Promise like middleware

By default all app steps will be finished. Except one of them has rejected promise:

app.then(async ctx => {
  
  ctx.res.statusCode = 200;
  ctx.res.setHeader('Content-Type', 'application/json');
  ctx.res.end('{"status":"OK"}');
  
  return app.reject();
  
}).then(async () => {
  
  // it won't work

}).catch(async () => {

  // it won't work

});

Using router

npm i -S yeps-router

app.js

const App = require('yeps');    
const Router = require('yeps-router');

const app = new App();
const router = new Router();

router.get('/').then(async ctx => {
  ctx.res.statusCode = 200;
  ctx.res.setHeader('Content-Type', 'application/json');
  ctx.res.end('{"status":"OK"}');     
});

app.then(router.resolve());

Using server

npm i -S yeps-server

bin/www

#!/usr/bin/env node
    
const app = require('../app');
const server = require('yeps-server');

const app = new App();

server.createHttpServer(app);

YEPS documentation

install

npm i yeps

Downloadsweekly downloads

110

version

1.0.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability