簡體   English   中英

Node.js server.listen回調嗎?

[英]Node.js server.listen callback?

我有一個node.js應用程序,一旦服務器開始偵聽,我需要運行一個命令。 server.listen上的文檔說:
server.listen(port, [hostname], [backlog], [callback])
但是當我嘗試使用這種格式時,代碼沒有運行,但是沒有錯誤消息出現。 這是我的應用程序的偵聽部分:

var spawn = require('child_process').spawn
function listen(port) {
    try {
        server.listen(port, "localhost",511, function() {
          spawn("open",["http://localhost:"+port+"/"])
        })
    } catch (e) {
        listen(port+1)
    }
}



你們中的一些人要求查看我的代碼的全部內容,因此這里是:

 var http = require("http"), path = require("path"), fs = require("fs"), mime = require("mime"), port = 1 var server = http.createServer(function(req, resp) { if (req.url == "/action" && req.headers["command"]) { resp.writeHead(200, { "Content-Type": "text/plain" }); console.log("Command sent: " + req.headers["command"]) try { var out = eval(req.headers["command"]) if (typeof out == "object") { var cache = []; out = JSON.stringify(out, function(key, value) { if (typeof value === 'object' && value !== null) { if (cache.indexOf(value) !== -1) { return "[Circular]"; } // Store value in our collection cache.push(value); } return value; }); } resp.end(out); } catch (e) { resp.end(e.stack) } } var local = __dirname + "/public" + req.url if (fs.existsSync(local)) { if (fs.lstatSync(local).isDirectory(local)) { if (fs.existsSync(local + "/index.html")) { local += "/index.html" resp.writeHead(200, { "Content-Type": mime.lookup(local) }); fs.readFile(local, function(err, data) { if (err) { resp.writeHead(500, { "Content-Type": "text/plain" }); resp.end("Internal server error"); throw err; } resp.end(data) }); } else { server.status_code = 403 resp.writeHead(403, { "Content-Type": "text/plain" }); resp.end("GET 403 " + http.STATUS_CODES[403] + " " + req.url + "\\nThat Directory has no Index") console.log("GET 403 " + http.STATUS_CODES[403] + " " + req.url) } } else { resp.writeHead(200, { "Content-Type": mime.lookup(local) }); fs.readFile(local, function(err, data) { if (err) { resp.writeHead(500, { "Content-Type": "text/plain" }); resp.end("Internal server error"); throw err; } resp.end(data) }); } } else { if (req.url != "/action") { server.status_code = 404 resp.writeHead(404, { "Content-Type": "text/plain" }); resp.end("GET 404 " + http.STATUS_CODES[404] + " " + req.url + "\\nThat File Cannot be found") console.log("GET 404 " + http.STATUS_CODES[404] + " " + req.url) } } }); var spawn = require('child_process').spawn function listen(port) { try { server.listen(port, "localhost") } catch (e) { listen(port+1) } } 

解決了!

結合@mscdex和Peter Lyons的答案后,我已經解決了這個問題。

 var spawn = require('child_process').spawn server.listen(0,"localhost", function(err) { if(err) throw err; spawn("open",["http://localhost:"+server.address().port+"/"]) }) 

謝謝你們倆

var spawn = require('child_process').spawn

function listen(port) {
  //Don't need try/catch here as this is an asynchronous call
  server.listen(port, "localhost", function(error) {
    if (error) {
      console.error("Unable to listen on port", port, error);
      listen(port + 1);
      return;
    }
    spawn("open",["http://localhost:"+port+"/"])
}
  • 不確定要閱讀的文檔是什么,但是server.listen的官方node.js文檔說server.listen(port, [host], [callback])
  • 您不需要try / catch,因為這是一個異步調用,它將通過回調的第一個參數指示錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM