npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-as-promised

v1.0.0

Published

This is simply the Express we all know and love promisified.

Downloads

23

Readme

Express as promised Build Status

This is simply the Express we all know and love with a few enhancements to support returning various values including promises.

So instead of:

app.get('/', function(request, response) {
  return quote.fetch().then(function(quote) {
    response.send(quote);
  });
});

We can simply just return the promise:

app.get('/', function() {
  return quote.fetch();
});

Both will result in something like:

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 87
Date: Sat, 12 Jul 2014 14:40:14 GMT
Connection: keep-alive

{quote: "The true measure of a man is how he treats somebody that can do him no good."}

Returning values

You can return strings and objects or their promised equivalent.

Promises

app.get('/', function() {
  var promise = bluebird.resolve('Hello world');
  return promise;
});
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 11
Date: Sat, 12 Jul 2014 14:40:14 GMT
Connection: keep-alive

Hello world

Strings

app.get('/', function( {
  return 'Hello world';
})
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 11
Date: Sat, 12 Jul 2014 14:40:14 GMT
Connection: keep-alive

Hello world

Errors and production

If your callback throws or returns an error a stack trace will be sent, for example:

app.get('/', function() {
  throw new Error('Something went wrong.');
});
HTTP/1.1 500 Internal Server Error
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 1059
Date: Sat, 12 Jul 2014 14:41:56 GMT
Connection: keep-alive

Error: Something went wrong
    at /Users/michael/Projects/express-as-promised/tests.js:5:9
    at /Users/michael/Projects/express-as-promised/main.js:13:19
    at Object._callback [as handle] (/Users/michael/Projects/express-as-promised/main.js:29:9)
    at next_layer (/Users/michael/Projects/express-as-promised/node_modules/express/lib/router/route.js:113:13)
    at Route.dispatch (/Users/michael/Projects/express-as-promised/node_modules/express/lib/router/route.js:117:5)
    at /Users/michael/Projects/express-as-promised/node_modules/express/lib/router/index.js:222:24
    at Function.proto.process_params (/Users/michael/Projects/express-as-promised/node_modules/express/lib/router/index.js:288:12)
    at next (/Users/michael/Projects/express-as-promised/node_modules/express/lib/router/index.js:216:19)
    at Layer.expressInit [as handle] (/Users/michael/Projects/express-as-promised/node_modules/express/lib/middleware/init.js:23:5)
    at trim_prefix (/Users/michael/Projects/express-as-promised/node_modules/express/lib/router/index.js:263:17)

Turning off errors in production

Unless NODE_ENV is set to production, then you'll just get:

HTTP/1.1 500 Internal Server Error
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 20
Date: Sat, 12 Jul 2014 14:45:51 GMT
Connection: keep-alive

Interal Server Error

Custom status codes

You can still use a custom status code when required:

app.get('/', function(req, res) {
  res.status(403);
  return 'Not allowed';
});
HTTP/1.1 403 Forbidden
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 11
Date: Sun, 13 Jul 2014 05:17:03 GMT
Connection: keep-alive

Not allowed

Can be used just like Express

And everything you're doing right now with Express, should just work.

app.get('/', function(req, res, next) {
  res.status(403);
  next();
}, function(req, res) {
  res.send('Hello world');
});

or even:

app.get('/', function(req, res, next) {
  res.status(403);
  next();
}, function(req, res) {
  return 'Hello world';
});

Tests

Just simply run npm test