Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

yeps

0.0.7 • 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

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 yaps:

npm i -S yaps

app.js

const App = require('yaps');

const app = module.exports = new App();

app.then(async ctx => {
  ctx.res.writeHead(200, {'Content-Type': 'text/plain'});
  ctx.res.end('okay');
});

app.catch(async (err, ctx) => {
  ctx.res.writeHead(500);
  ctx.res.end(err.message);
});

bin/www

#!/usr/bin/env node

const http = require('http');
const app = require('../app');

const server = http.createServer(app.resolve());

server.listen(process.env.PORT);

package.json

"scripts": {
  "start": "NODE_ENV=production PORT=3000 node --harmony 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.writeHead(200);
  ctx.res.end('test');
  
  return app.reject(); // or return Promise.reject()
});

app.then(async () => {
  // it wont work
});

app.catch(async () => {
  // it wont work
});

install

npm i yeps

Downloadsweekly downloads

92

version

0.0.7

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability