1

我是 node.js 的新手,我正在尝试创建一个 http 服务器并生成一个子进程。

看代码

var server = http.createServer(function(req, res) {
res.writeHead(200);
  switch(req.url) {

        case '/vm/start':
            req.on('data', function(data) {             
            console.log("hello");
                console.log(data.toString());
                exec('CALin.exe', function(err, data) {     
                    console.log(err)
                    console.log(data.toString());


});
                    res.end('');
                });
            });

            break;
    }
});

server.listen(9090);
console.log("Server running on the port 9090");

当我尝试使用此 url 运行上述代码http://172.16.2.51:9090/vm/start时,我什么也没得到。浏览器显示连接重置问题我不知道为什么会这样

node server.js

Server running on the port 9090
4

2 回答 2

1

There is no data being sent in the request so the data event does not get triggered and therefore res.end never gets called and the request times out. Add a listener for the requests end event and then call response.end.

于 2013-10-31T09:16:02.170 回答
0

将代码写入 js 文件并运行

var http = require("http");
var server = http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<!DOCTYPE "html">");
response.write("<html>");
response.write("<head>");
response.write("<title>Hello World Page</title>");
response.write("</head>");
response.write("<body>");
response.write("Hello World!");
response.write("</body>");
response.write("</html>");
response.end();
});

server.listen(9090);
console.log("Server is listening");

将此代码运行为

 node web_server.js
于 2013-10-31T09:08:02.533 回答