-
Notifications
You must be signed in to change notification settings - Fork 15k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request that makes anything runnable #2874
Conversation
We use a similar pattern but for our apps in an SOA architecture, so not on a per middleware basis. It is a nice pattern, BUT, I think this would make a better third party module that wraps express, not necessarily a part of core. But that's just my 2c. EDIT: And if you make it I would love to use it, because we have yet to get around to abstracting that out of our apps. :) |
bca2432
to
78340be
Compare
I think it's good if it's part of core because then it will be documented on http://expressjs.com/. That will add awareness to people how to make good use of express. EDIT: Thanks for feedback @wesleytodd :) |
I was thinking of another method name |
Express' entire direction is a reduction of API to increase simplicity and expandability. Express is also moving away from the idea of mounted applications, favoring routers instead. This idea directly conflicts with both of those goals. I agree with @wesleytodd, this belongs in a third party lib. |
@ChiperSoft Where can I read about moving away from mounting applications? I would say that is one of the best feature with express. I would probably move away from express if that gets deprecated. 👎 Anyway thanks for feedback! 😄 |
I think this method brings more to the table than |
I don't know of any specific places it's written about. It's the impression that I got from conversations with @dougwilson and from his comments in various issues and pull requests. |
Hey @EloB, I was using this pattern today and ended up just publishing a module that does this: https://github.com/wesleytodd/runnable Does that solve your problems? |
@wesleytodd except that you can't have multiple runnable instances. If something requires runnable and it's resolves |
@EloB Do you have an example? I would love to make sure that module works for it. I just used it in the case similar to what I have been doing, which is a bin script and an entry point which is runnable. |
Seems to me that in the future this would mostly be possible with routers themselves, so I'm not sure I see the value, though I may have missed something. |
Hey @Fishrock123, this specific feature makes the script directly runnable or "requireable". For example: // index.js
module.exports = runnable(function () {
var app = express();
return app.listen();
}); // bin/start
#! /usr/local/env node
var app = require('../index');
// do some other configuration loading stuff, or like we use this
// pattern for register with Consul our service discover layer
// Then start server
app(); With a setup like this you can run I think @EloB's use case is something more like microservices where you can run one directly, or compose them together into a larger application. Correct me if I am wrong @EloB. |
@wesleytodd @Fishrock123: You are right. Project tends to start small and then grow over time. If you structure your app to have everything as microservice. Then you could easily start a piece of your application in a separate process without any overhead. You don't need to refactor your application either and together with docker for instance this is awesome. If I get some more spare time I could show how this could be used together with Docker. |
78340be
to
0131da2
Compare
This is a draft for a new method that I call
app.runnable()
. It's almost works the same asapp.listen()
but it will only start the server if the current file is therequire.main
.I always create my middlewares as express instances instead of just
module.exports = function(req, res, next) {};
. I also add this snippet in the bottom section of all my middlewares:I really like this pattern because it's easy to run any middleware as a service with just
node middlewares/something.js
. If you add some environment variables then it's really easy to scale a specific area of your application as well.I haven't written any tests because I want to feedback from you guys what you think about this feature before I will write them.
Examples
Example 1
Example 2
Example 3