Skip to content
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

Closed
cly opened this issue Aug 5, 2014 · 12 comments
Closed

Shut down server (something similar to expressjs close() method) #328

cly opened this issue Aug 5, 2014 · 12 comments

Comments

@cly
Copy link

cly commented Aug 5, 2014

Is there any way to shut down server programmatically or to stop listening on the port?

@jonathanong
Copy link
Member

just wrap app.callback() in http.createServer() yourself

@cly
Copy link
Author

cly commented Aug 5, 2014

Cool thanks for the quick response! Works great.

@marcusoftnet
Copy link

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?

@marcusoftnet
Copy link

Hmmm... I got this to work, by exposing the server object returned by app.listen().

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 app.listen() in my test:

  • one in the server itself
  • one in the test code creating the supertest agent var supertest = require("supertest").agent(app.listen());

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.

@limianwang
Copy link

@marcusoftnet

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

@marcusoftnet
Copy link

Oooh - nice.
Does that mean that it's closing automatically, or that it just gets a new port number for that particular request?

That works perfectly! Thanks a bunch

@limianwang
Copy link

@marcusoftnet
Copy link

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.
Sorry

@marcusoftnet
Copy link

@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.

@limianwang
Copy link

In your server.js, make sure you only listen when the app.js is run

if (!module.parent) {
   app.listen();
}

module.exports = app;

@marcusoftnet
Copy link

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

@marcusoftnet
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants