Express as promised
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;
We can simply just return the promise:
app;
Both will result in something like:
HTTP/1.1 200 OKX-Powered-By: ExpressContent-Type: text/html; charset=utf-8Content-Length: 87Date: Sat, 12 Jul 2014 14:40:14 GMTConnection: 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;
HTTP/1.1 200 OKX-Powered-By: ExpressContent-Type: text/html; charset=utf-8Content-Length: 11Date: Sat, 12 Jul 2014 14:40:14 GMTConnection: keep-alive Hello world
Strings
app
HTTP/1.1 200 OKX-Powered-By: ExpressContent-Type: text/html; charset=utf-8Content-Length: 11Date: Sat, 12 Jul 2014 14:40:14 GMTConnection: keep-alive Hello world
Errors and production
If your callback throws or returns an error a stack trace will be sent, for example:
app;
HTTP/1.1 500 Internal Server ErrorX-Powered-By: ExpressContent-Type: text/html; charset=utf-8Content-Length: 1059Date: Sat, 12 Jul 2014 14:41:56 GMTConnection: 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 ErrorX-Powered-By: ExpressContent-Type: text/html; charset=utf-8Content-Length: 20Date: Sat, 12 Jul 2014 14:45:51 GMTConnection: keep-alive Interal Server Error
Custom status codes
You can still use a custom status code when required:
app;
HTTP/1.1 403 ForbiddenX-Powered-By: ExpressContent-Type: text/html; charset=utf-8Content-Length: 11Date: Sun, 13 Jul 2014 05:17:03 GMTConnection: keep-alive Not allowed
Can be used just like Express
And everything you're doing right now with Express, should just work.
app;
or even:
app;
Tests
Just simply run npm test