简体   繁体   中英

Server commands (or equivalent) in node.js

I have a server up and running in my digitaldomain droplet. in my server code, I have a function called userCount(); which simply returns the number fo connected users.

I do not want to console.log the number of users on my server, each time someone is connected. This just creates a mess. Instead, I would like to be able to run this command whenever I need to see the "current user count".

How can I make my server in a way that, I will also be able to input commands to it (from the console) whenever it's needed?

What is the best and/or most optimal way of doing this?

How about exporting the relevant function and execute it whenever you feel like it?

droplet.js

...
function userCount() {
    return 42;
}
...
module.exports = {
    userCount: userCount
}

Create a wrapper file:

wrapper.js

var connected = require('./droplet.js');
console.log(connected.userCount());

Execute that file from the command line:

> node wrapper

If you do not want to create an additional file, use the node interface:

> node
console.log(require('./droplet.js').userCount());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM