I'm writing a test script for my module and I need to be able to close my server when my request is done. The following code works but I can see that app.close() was dropped from express 3. What's the best way to do it now?
var testCase = require('nodeunit').testCase;
var request = require('request');
var express = require('express');
var app = express.createServer();
var srv = app.listen();
....
request({
method: 'POST',
json: true,
body: { id: 'second request'},
url: 'http://' + target
}, function(err, res, body) {
console.info("closing server");
app.close();
test.done();
});
});
Thanks, Li
p.s. test.done() must be called after closing the server, otherwise the test will fail.
Express applications used to inherit from http.Server
, which they no longer do, and that's where the close
method came from. Instead, call close()
on your srv
instance. You may commonly see this code written as:
var app = express.createServer();
var srv = require('http').createServer(app);
srv.listen(port);
According to the documentation for app.listen()
:
Bind and listen for connections on the given host and port, this method is identical to node's
http.Server#listen()
.