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 theclosemethod came from. Instead, callclose()on yoursrvinstance. You may commonly see this code written as:According to the documentation for
app.listen():