Reputation: 4350
I see plenty of examples of how to create a server with Connect, but how do I close it gracefully without ctrl + c?
My use case is to spin up a lightweight connect server for testing/mocking purposes.
Upvotes: 0
Views: 145
Reputation: 2153
Connect uses Node's build in HTTP library, so you can just call server.close(): http://nodejs.org/api/http.html#http_server_close_callback. You can also just exit the app with process.exit(0); to return to the shell that started your app.
keep in mind http.close() just stops accepting new connections, depending on how your app is structured it still may not exit. Existing connections will be serviced until they close themselves.
Here's an example that uses Connect and http intermingled:
var connect = require('connect')
, http = require('http');
var app = connect()
.use(connect.favicon())
.use(connect.logger('dev'))
.use(connect.static('public'))
.use(connect.directory('public'))
.use(connect.cookieParser())
.use(connect.session({ secret: 'my secret here' }))
.use(function(req, res){
res.write('Hello from Connect!\n');
res.end();
//stop accepting new connections:
srv.close();
//exit the app too:
process.exit(0);
});
var srv = http.createServer(app).listen(3000);
Upvotes: 1
Reputation: 4350
I just realized instead of writing the server this way:
var app = connect()
.use(function(req, res, next){
res.end('hello world')
})
.listen(3000);
I can create the server this way:
var app = connect()
.use(function(req, res, next){
res.end('hello world')
});
var server = http.createServer(app).listen(3000, done);
Thus, allowing me to use server.close();
Upvotes: 0