Node.js readline同步冻结我的服务器(windows)
Node.js readline-sync freeze my server (windows)
Node.js readline sync暂停我的服务器,我该如何做到服务器在等待命令时工作并打印我的信息?
也许是多线程的东西,或者2 cmd,但不太好。
这是我的服务器代码:
'use strict';
var http = require('http'),
fs = require('fs');
var PORT = 8080;
var requestIp = require('request-ip');
var os = require('os');
var hostname = os.hostname();
var readlineSync = require('readline-sync');
//get client ip
var ipMiddleware = function(request, response) {
var clientIp = requestIp.getClientIp(request);
return clientIp.slice(7);
};
//print function
function msg(msg){
msg = msg.charAt(0).toUpperCase() + msg.slice(1);
console.log("> "+msg);
console.log();
}
//command function
function inpt(){
var command = readlineSync.question('> ');//this is the line that freezing all
console.log();
if(command === "exit" || command === "stop" || command === "end" ){
msg(command+"ing server...");
process.exit();
}if(command.substring(0, 14) === "switch port to " && command.length === 18 && parseInt(command.substring(14,18)).isInteger > 0){
PORT = parseInt(command.substring(14,18));
msg("port changed to "+PORT);
http.createServer(onRequest).listen(PORT);
msg("LAN server running on "+addresses[0]+":"+PORT);
}
else{
msg("sorry but "+command+" is an unknown command.");
}
//after receiving command its's ask again for input
inpt();
}
//get server ip
var interfaces = os.networkInterfaces();
var addresses = [];
for (var k in interfaces) {
for (var k2 in interfaces[k]) {
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal) {
addresses.push(address.address);
}
}
}
//404 Response
function send404Response(request,response) {
msg("Error 404 "+ipMiddleware(request,response));
response.writeHeader(404, {"Content-Type": "text/html"});
fs.createReadStream('./404.html').pipe(response);
}
//write html file to client by name
function sendFileResponse(name,request,response) {
msg(name+" page "+ipMiddleware(request,response));
response.writeHeader(200, {"Content-Type": "text/html"});
fs.createReadStream('./'+name+'.html').pipe(response);
}
//handle request
function onRequest(request, response) {
if(request.method == 'GET' && request.url == '/'){
msg("Home page "+ipMiddleware(request,response));
response.writeHeader(200, {"Content-Type": "text/html"});
fs.createReadStream('./index.html').pipe(response);
}else if(request.url.length > 1){
var name = request.url.slice(1);
if(fs.existsSync("./"+name+".html")){
sendFileResponse(name,request,response);
}else{
send404Response(request,response);
}
}else{
send404Response(request,response);
}
}
//start
msg("Server ip: "+addresses[0]);
msg("Hostname: "+hostname);
http.createServer(onRequest).listen(PORT);
msg("LAN server running on "+addresses[0]+":"+PORT);
//on start the cmd will ask for commands
inpt();
readlineSync()
是同步的。根据定义,一切都将等待,直到调用结束。如果不能实现这种行为,请使用readline()
。
请参阅https://nodejs.org/api/readline.html#readline_rl_question_query_callback
你可以试试这样的东西:
function inpt(){
readline.question('> ', function( command ) {
console.log();
if(command === "exit" || command === "stop" || command === "end" ){
msg(command+"ing server...");
process.exit();
}if(command.substring(0, 14) === "switch port to " && command.length === 18 && parseInt(command.substring(14,18)).isInteger > 0){
PORT = parseInt(command.substring(14,18));
msg("port changed to "+PORT);
http.createServer(onRequest).listen(PORT);
msg("LAN server running on "+addresses[0]+":"+PORT);
}
else{
msg("sorry but "+command+" is an unknown command.");
}
//after receiving command its's ask again for input
inpt();
}
});
您可能需要对内部进行一些修改,我只是将其余的代码放入回调中,这样它就会在分配命令后运行。
相关文章:
- 使用JSP从服务器检索和显示图像
- 如何从windows控制台停止webpack开发服务器
- jQuery.parseJSON()未在Windows服务器上解码有效的Json字符串
- Windows.location.href 是否具有特定于服务器的行为
- Javascript 在 Windows 上本地工作,但在 Web 服务器上不工作
- 使用PHP'将网站打包为Windows可执行文件;的内置服务器
- gulp-rsync在Windows上,而不是在远程Linux服务器上制作目录
- 编辑文件重新启动服务器在流星在windows
- 流星在windows -服务器崩溃的代码推送
- 流星js Windows安装错误"未能联系安装服务器"
- Node.js服务器赢得't在windows 7 64位上启动
- 从Windows的web服务器迁移到Linux
- 如何在安装了apache服务器的Windows上运行node.js
- 运行Node.js(在Windows R2服务器上):我如何减少我的grunt任务消耗的RAM量
- 我如何允许节点pg模块与我的Windows postgresql服务器通信
- 在Java上编写WebSocket服务器.Windows 7 64位上的问题
- Node.js readline同步冻结我的服务器(windows)
- Websockets - 服务器端要求(Windows 服务器,而不是 Apache)
- Windows 8将图像发送到web服务器
- 从windows服务器获取文件到linux服务器