Js 服务器,接受 POST 请求

我正在尝试允许 javascript 与 Node.js 服务器通信。

POST 请求(网络浏览器)

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);

先谢谢你的帮助。

168386 次浏览

下面的代码演示如何从 HTML 表单读取值。正如@pilvdb 所说,您需要使用 request. on (‘ data’...)来捕获主体的内容。

const http = require('http')


const server = http.createServer(function(request, response) {
console.dir(request.param)


if (request.method == 'POST') {
console.log('POST')
var body = ''
request.on('data', function(data) {
body += data
console.log('Partial body: ' + body)
})
request.on('end', function() {
console.log('Body: ' + body)
response.writeHead(200, {'Content-Type': 'text/html'})
response.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>
</html>`
response.writeHead(200, {'Content-Type': 'text/html'})
response.end(html)
}
})


const port = 3000
const host = '127.0.0.1'
server.listen(port, host)
console.log(`Listening at http://${host}:${port}`)




如果您使用类似于 快递身体解析器的内容,那么它看起来应该是这样的,因为 Express 将处理 request.body 连接

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


app.use(express.bodyParser())


app.get('/', function(request, response) {
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>
</html>`
response.writeHead(200, {'Content-Type': 'text/html'})
response.end(html)
})


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


const port = 3000
app.listen(port)
console.log(`Listening at http://localhost:${port}`)


在 nodejs 中接收 POST 和 GET 请求:

1)服务器

    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();