javascript - 如何从 node.js Express 发送 POST 请求?

标签 javascript node.js http post express

谁能告诉我从 node.js Express 发送帖子请求的最简单方法,包括如何传递和检索一些数据?我期待类似于 PHP 中的 cURL 的东西。

最佳答案

var request = require('request');
 function updateClient(postData){
            var clientServerOptions = {
                uri: 'http://'+clientHost+''+clientContext,
                body: JSON.stringify(postData),
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            }
            request(clientServerOptions, function (error, response) {
                console.log(error,response.body);
                return;
            });
        }

为此,您的服务器必须类似于:

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())

var port = 9000;

app.post('/sample/put/data', function(req, res) {
    console.log('receiving data ...');
    console.log('body is ',req.body);
    res.send(req.body);
});

// start the server
app.listen(port);
console.log('Server started! At http://localhost:' + port);

关于javascript - 如何从 node.js Express 发送 POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32327858/

相关文章:

Node.js console.log() 没有记录任何东西

node.js - 在 firestore 中,连接用户调用云函数从云 firestore 获取数据的更好方法是什么?

http - JMeter中如何读取 "Graph Result"

git - 在 Tomcat 上设置远程 Git 仓库

http - 在 Golang 中没有缓冲 http.ResponseWritter

JavaScript:如何将按钮元素的值分配给另一个div元素?

javascript - HTML 获取图像字段的值并将该值用作图像字段的 src

javascript - 为什么具有已定义 Prop 的对象类型不是 Flow 中具有可选 Prop 的对象类型的子类型?

c# - 美国电话格式需要正则表达式

asp.net - Angular 4 : Can't bind to 'ngModel' since it isn't a known property of 'input'