我需要创建具有1个后端点的快速服务器,该端点获取POST数据并将其打印在启动服务器的控制台中:
var express = require("express");
var app = express();
app.get("/", function (req, res) {
res.send("GET request");
});
app.post("/", function (req, res) {
console.log(req.params);
res.send("POST request");
});
app.listen(2705, function () {
console.log("Server is running on port: " + 2705);
});
enter code here

如果请求来自其他本地服务器,则无法找到如何在控制台中打印此POST请求的解决方案。
答案 0 :(得分:1)
您使用的是req.params,它不是POST的主体。如果要查看使用POST发送的数据,则需要访问req.body。此外,快递需要被告知解析身体。这可以使用Express body-parser完成。
答案 1 :(得分:0)
var app = require('express')();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({
extended: true })); // for parsing application/x-www-form-urlencoded
app.post('/profile', upload.array(), function (req, res, next) {
console.log(req.body);
res.json(req.body);
});
答案 2 :(得分:0)
const express = require('express');
const bodyParser = require('body-parser');
const product = require('./routes/product.route'); // Imports routes for the products
const app = express();
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({extended: false})); // for parsing application/x-www-form-urlencoded
// In express.js the order in which you declare middleware is very important. bodyParser middleware must be defined early than your own middleware (api endpoints).
app.use('/products', product);// if you've mentioned routers
// solution you're looking for
app.post("/", function (req, res) {
console.log(req.body);// req data
res.send("POST request!!!");
});
let port = 8001;
app.listen(port, () => {
console.log('Server is up and running on port number ' + port);
});