Javascript 接受POST请求的Node.js服务器
我正在尝试允许javascript与Node.js服务器通信 发布请求(网络浏览器) 现在Node.js服务器代码如下所示。在用于GET请求之前。我不知道如何让它与POST请求一起工作 服务器(Node.js)Javascript 接受POST请求的Node.js服务器,javascript,node.js,nodes,Javascript,Node.js,Nodes,我正在尝试允许javascript与Node.js服务器通信 发布请求(网络浏览器) 现在Node.js服务器代码如下所示。在用于GET请求之前。我不知道如何让它与POST请求一起工作 服务器(Node.js) 提前感谢您的帮助。以下代码显示了如何从HTML表单读取值。正如@pimvdb所说,您需要使用request.on('data')来捕获正文的内容 const http = require('http') const server = http.createServer(function
提前感谢您的帮助。以下代码显示了如何从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}`)
consthttp=require('http'))
const server=http.createServer(函数(请求、响应){
console.dir(request.param)
if(request.method==“POST”){
console.log('POST')
变量体=“”
请求.on('data',函数(data){
body+=数据
console.log('部分正文:'+正文)
})
request.on('end',function(){
console.log('Body:'+Body)
writeHead(200,{'Content-Type':'text/html'})
response.end('post received')
})
}否则{
console.log('GET')
变量html=`
姓名:
`
writeHead(200,{'Content-Type':'text/html'})
response.end(html)
}
})
常数端口=3000
常量主机='127.0.0.1'
侦听(端口、主机)
log(`在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}`)
var express=require('express'))
var fs=require('fs')
var app=express()
app.use(express.bodyParser())
app.get('/',函数(请求,响应){
console.log('GET/'))
变量html=`
姓名:
`
writeHead(200,{'Content-Type':'text/html'})
response.end(html)
})
app.post(“/”,函数(请求,响应){
console.log('POST/'))
console.dir(request.body)
writeHead(200,{'Content-Type':'text/html'})
response.end('谢谢')
})
常数端口=3000
应用程序侦听(端口)
console.log(`listing athttp://localhost:${port}`)
在nodejs中接收POST和GET请求:
1) .服务器
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 POST请求可能有问题。当我尝试发出请求时,它没有在node.js服务器上接收数据。尽管我认为这是节点文件的正确代码。javascript是个问题我现在遇到的问题是javascript甚至不能发出请求。当我尝试发出请求时,node.js文件什么也不做。这可能是因为我之前包含的代码没有对POST请求发送任何响应(它只显示在服务器上,我们收到了一篇POST)。我已经更新了代码,以便在POST中实际响应,这就解决了问题。我还包括了我用来测试它的HTML(其中包括您的JavaScript代码),(err)检查去哪里了?作为req&res之前的变量,然后通过console.error(err.stack)通知用户;另外,这是一个非常完整的例子,我学到了很多东西,值得称赞。我意识到这个答案很古老,但我只想指出,BodyParser现在必须单独安装,以使表单操作假定为localhost,而实际上它可以/
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}`)
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");
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();