0

How can I have our node.js application halt/block, waiting on stdin before running the rest of the code? I have:

var readline = require('readline');

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

var ECRYPTION_KEY;
rl.question('Provide the global encryption key:', function(encyrptionKey) {
    ECRYPTION_KEY = encyrptionKey;
    rl.close();
});

var https = require('https');

https.createServer({ ... });
console.log("https server running...");

Right now, I see Provide the global encryption key: and also https server running.. immediately. I.E. does not block and wait on stdin before creating the https server.

1
  • 2
    Place the createServer function inside the callback for the question() function Commented Feb 1, 2014 at 3:18

2 Answers 2

1

The server is being created before the encryption key is received from the rl callback.

Sign up to request clarification or add additional context in comments.

Comments

0

You cannot do that. Ever. Node.js is designed to run without blocking all the time, and waiting for stdin to do something is a misuse.

PS: Actually you can (with fibers) but it is not a good reason to use them.

adeneo's advice is right, just put createServer function inside your callback.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.