节点.js接受 POST 请求的服务器
Node.js server that accepts POST requests
我试图允许javascript与Node.js服务器进行通信。
开机自检请求(网络浏览器)
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 请求一起使用。
服务器(节点.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);
下面的代码演示如何从 HTML 表单中读取值。正如@pimvdb所说,您需要使用 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.js 和 Bodyparser 这样的东西,那么它看起来像这样,因为 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();
相关文章:
- 如何检测第三方广告服务器请求
- Javascript onclick需要点击两次,然后会创建越来越多的服务器请求
- 新的会话cookie直到下一个服务器请求才提供
- 将所有angular2捆绑包包含在index.html中会使导入从内存获取数据,而不是再次从服务器请求数据
- web工作程序中不同的服务器请求行为
- 如何使用
- 使用angular js通过代理服务器请求Http
- 正在使用node.js服务器请求文件
- 每x秒从服务器请求数据的最佳方法
- 如何在调用服务器请求时删除错误
- 我应该在我的PhoneGap应用程序中包括我的所有源,还是从远程服务器请求它们
- 节点.js简单的 Web 服务器请求生命周期
- 使用 jQuery 从 NodeJS 服务器请求数据
- 如何从HTTPS服务器请求HTTP服务器的javascript文件
- 如何在没有持续服务器请求的情况下在 AngularJS 中进行视图重定向
- 角度验证:如果用户输入无效的电子邮件或密码,则限制服务器请求
- AJAX 从服务器请求另一个 JavaScript 或函数
- AngularJs.路由.处理服务器请求.很少有问题
- WebWorkers-新Worker不断从服务器请求相同的脚本
- 从服务器请求JSON并用Javascript进行解析