Reputation: 9326
Tinkering with a command land NPM app. It's only 12 lines and I'm stuck. Once I enter into my promise, the app continues to run rather than letting me type into command line again (until cmd+C of course). Not sure what to google to find answer myself so hopefully code below is enough!
#! /usr/bin/env node
var MongoClient = require('mongodb').MongoClient;
var dbUrl = 'mongodb://localhost:27017/building';
console.log(process.argv);
MongoClient.connect(dbUrl)
.then(function(db){
console.log('connected to db!');
// I guess there is some command to run here?
// Promise resolution of sorts?
return;
});
Upvotes: 2
Views: 1653
Reputation: 707916
In node.js, the process will continue to run by itself if you have timers still running, servers still running or sockets still open.
You can manually exit your program at any time with process.exit()
even if some of the above items are still running/open.
Or, you can unref()
any of the above three items to instruct node.js to NOT wait for that specific resource to be closed before exiting the program.
So, in your specific case with the DB connection, you can:
process.exit()
to manually exit the program..unref()
on the DB socket to tell node.js not to wait for this resource.FYI, this is a very similar question: mongoclient.connection does not return cursor back on command line
Upvotes: 5
Reputation: 146114
Node will keep running until you explicitly disconnect from mongodb. To do that, call db.close()
.
Upvotes: 1