-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Shut down server (something similar to expressjs close() method) #328
Comments
just wrap |
Cool thanks for the quick response! Works great. |
Sorry, but I didn't follow that... Could you supply a code example on how to do that? I'm experiencing some problems running mocha watching my test, when I start mocha from a package.json script using npm. I think shutting the server down in my test tear down would do the trick. Here's my test now: var http = require('http');
var app = require("../");
var supertest = require("supertest").agent(app.listen());
describe("Our amazing site", function () {
it("has a nice welcoming message", function (done) {
request
.get("/")
.expect("Koa says Hi!")
.end(done);
});
}); How could I "just wrap app.callback() in http.createServer() yourself" in that code? Or is there another way to stop listening / shutting down the server? |
Hmmm... I got this to work, by exposing the server object returned by var app = require("koa")();
app.use(function *(){
this.body = "Koa says Hi!";
});
var server = app.listen(3000);
console.log("Application started. Listening on port:" + 3000);
var exportObj = {
server : server,
app : app
};
module.exports = exportObj; In my test I can now do a close on the server: var app = require("../").app;
var server = require("../").server;
var request = require("supertest").agent(server);
describe("Our amazing site", function () {
after(function (done) {
server.close();
done();
});
}); Is this ... ugly? Is the suggested solution above a better solution to my problem? My feeling was that, before I was doing 2
I'm convinced that this is something that I have had working before, without any after-hooks or exporting of server objects. I'm on Node 4.0.0 now, btw. |
I believe it'll be something like this for the sample you've provided. var http = require('http');
var app = require("../");
var request = require("supertest")
describe("Our amazing site", function () {
it("has a nice welcoming message", function (done) {
request(app.listen())
.get("/")
.expect("Koa says Hi!")
.end(done);
});
}); Take a look at how koa developers have their test setup, it's pretty neat: https://github.com/koajs/koa/blob/master/test/application.js |
Oooh - nice. That works perfectly! Thanks a bunch |
Strike that - spoke to soon. Your example above still gives me the EADDRINUSE... And that only happens when I run it via npm. running the same command from the prompt works fine. Crap - I don't get this. |
@limianwang but I see your example, and I think I get that. In the end method it tries to close the server. That server is what I pass as a parameter when creating the supertest request object. but ... I still get the pesky address problem. |
In your server.js, make sure you only if (!module.parent) {
app.listen();
}
module.exports = app; |
AHA! That's something that I did not know. There was something like that I was looking for. Perfect - I have a few options now. Thanks a bunch for your help @limianwang! You save me and my sleep tonight :P |
I wrote the whole thing down in a blog post, should anyone want to know more. What I still fail to grasp is why this only failed when run as a script in npm. Running the same mocha command from the prompt worked fine |
Is there any way to shut down server programmatically or to stop listening on the port?
The text was updated successfully, but these errors were encountered: