2012-08-17 170 views
35

我试图让javascript与Node.js服务器进行通信。接受POST请求的Node.js服务器

POST请求(JavaScript)的

var http = new XMLHttpRequest(); 
var params = "text=stuff"; 
http.open("POST", "http://someurl.net:8080", true); 

http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
http.setRequestHeader("Content-length", params.length); 
http.setRequestHeader("Connection", "close"); 

alert(http.onreadystatechange); 
http.onreadystatechange = function() { 
    if (http.readyState == 4 && http.status == 200) { 
    alert(http.responseText); 
    } 
} 

http.send(params); 

眼下的Node.js服务器的代码如下所示。它用于GET请求之前。我不知道如何使它与POST请求一起工作。

服务器(Node.js的)

var server = http.createServer(function (request, response) { 
    var queryData = url.parse(request.url, true).query; 

    if (queryData.text) { 
    convert('engfemale1', queryData.text, response); 
    response.writeHead(200, { 
     'Content-Type': 'audio/mp3', 
     'Content-Disposition': 'attachment; filename="tts.mp3"' 
    }); 
    } 
    else { 
    response.end('No text to convert.'); 
    } 
}).listen(8080); 

在此先感谢您的帮助。

+1

我相信你必须使用'request'的'data' /'end'事件。这适用于我:http://pastebin.com/6aKv7WHJ。不知道这是否是真正的做法。 – pimvdb 2012-08-17 13:20:05

+0

我认为javascript POST请求可能有问题。当我尝试发出请求时,它不会在node.js服务器上接收数据。 – 2012-08-17 13:51:56

+1

虽然我认为这是节点文件的正确代码。 javascript是问题 – 2012-08-17 14:05:38

回答

75

以下代码显示了如何从HTML表单读取值。由于@ pimvdb说你需要使用request.on('data'...)来捕获正文的内容。

http = require('http'); 
fs = require('fs'); 
server = http.createServer(function(req, res) { 

    console.dir(req.param); 

    if (req.method == 'POST') { 
     console.log("POST"); 
     var body = ''; 
     req.on('data', function (data) { 
      body += data; 
      console.log("Partial body: " + body); 
     }); 
     req.on('end', function() { 
      console.log("Body: " + body); 
     }); 
     res.writeHead(200, {'Content-Type': 'text/html'}); 
     res.end('post received'); 
    } 
    else 
    { 
     console.log("GET"); 
     //var html = '<html><body><form method="post" action="http://localhost:3000">Name: <input type="text" name="name" /><input type="submit" value="Submit" /></form></body>'; 
     var html = fs.readFileSync('index.html'); 
     res.writeHead(200, {'Content-Type': 'text/html'}); 
     res.end(html); 
    } 

}); 

port = 3000; 
host = '127.0.0.1'; 
server.listen(port, host); 
console.log('Listening at http://' + host + ':' + port); 

如果你使用类似Express.js那么就可以简化为这样的事情,因为快递需要照顾很多HTTP管道的你:

var express = require('express'); 
var fs = require('fs'); 
var app = express(); 

app.use(express.bodyParser()); 

app.get('/', function(req, res){ 
    console.log('GET /') 
    //var html = '<html><body><form method="post" action="http://localhost:3000">Name: <input type="text" name="name" /><input type="submit" value="Submit" /></form></body>'; 
    var html = fs.readFileSync('index.html'); 
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    res.end(html); 
}); 

app.post('/', function(req, res){ 
    console.log('POST /'); 
    console.dir(req.body); 
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    res.end('thanks'); 
}); 

port = 3000; 
app.listen(port); 
console.log('Listening at http://localhost:' + port) 

在这两种情况下,我正在读“ index.html的”,这是与JavaScript的一个非常基本的HTML文件正在使用:在

的NodeJS
<html> 
<body> 
    <form method="post" action="http://localhost:3000"> 
     Name: <input type="text" name="name" /> 
     <input type="submit" value="Submit" /> 
    </form> 

    <script type="text/JavaScript"> 
     console.log('begin'); 
     var http = new XMLHttpRequest(); 
     var params = "text=stuff"; 
     http.open("POST", "http://localhost:3000", true); 

     http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
     //http.setRequestHeader("Content-length", params.length); 
     //http.setRequestHeader("Connection", "close"); 

     http.onreadystatechange = function() { 
      console.log('onreadystatechange'); 
      if (http.readyState == 4 && http.status == 200) { 
       alert(http.responseText); 
      } 
      else { 
       console.log('readyState=' + http.readyState + ', status: ' + http.status); 
      } 
     } 

     console.log('sending...') 
     http.send(params); 
     console.log('end'); 

    </script> 

</body> 
</html> 
+0

我现在遇到的问题是JavaScript甚至无法提出请求。当我尝试发出请求时,node.js文件不执行任何操作。 – 2012-08-17 14:31:56

+2

这可能是因为我包含的前面的代码没有发送POST请求的任何响应(它只显示在我们收到POST的服务器上)。我更新了代码以实际在POST中作出响应,照顾它。我还包括了我用来测试它的HTML(其中包括您的JavaScript代码) – 2012-08-17 15:09:55

+0

非常感谢这项工作。 – 2012-08-17 15:14:04

4

接收POST和GET请求:

1).Server

var http = require('http'); 
    var server = http.createServer (function(request,response){ 

    response.writeHead(200,{"Content-Type":"text\plain"}); 
    if(request.method == "GET") 
     { 
      response.end("received GET request.") 
     } 
    else if(request.method == "POST") 
     { 
      response.end("received POST request."); 
     } 
    else 
     { 
      response.end("Undefined request ."); 
     } 
}); 

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

2)。客户端:

var http = require('http'); 

var option = { 
    hostname : "localhost" , 
    port : 8000 , 
    method : "POST", 
    path : "/" 
} 

    var request = http.request(option , function(resp){ 
     resp.on("data",function(chunck){ 
      console.log(chunck.toString()); 
     }) 
    }) 
    request.end();