[英]Read and block from stdin in node.js
How can I have our node.js application halt/block, waiting on stdin
before running the rest of the code? 我如何在运行其余代码之前让我们的node.js应用程序暂停/阻塞,在
stdin
上等待? 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. 现在,我看到立即
Provide the global encryption key:
和“ https server running..
。 IE does not block and wait on stdin
before creating the https
server. 在创建
https
服务器之前,IE不会阻止并等待stdin
。
在从rl
回调接收到加密密钥之前,正在创建服务器。
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.
Node.js的设计宗旨是始终不阻塞地运行,而等待stdin做某事是一种滥用。
PS: Actually you can (with fibers) but it is not a good reason to use them. PS:实际上,您可以(使用纤维),但这不是使用它们的充分理由。
adeneo's advice is right, just put createServer function inside your callback. adeneo的建议是正确的,只需将createServer函数放入回调中即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.