You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Express+Node.js实现在Postman中直接打印前端提交的用户详情

解决Postman中无法直接打印提交后响应的问题

当前你的/submit接口在完成文件写入后执行了res.redirect("/"),这会返回302重定向状态码,Postman默认会自动跟随重定向请求,因此你看到的是首页内容,而非提交操作对应的响应结果。

要让Postman直接打印用户响应,只需把重定向逻辑改为返回明确的响应内容即可,示例修改如下:

import express from "express";
import { dirname } from 'path';
import { fileURLToPath } from "url";
import bodyParser from "body-parser";
import fs from "fs";

const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();

app.use(bodyParser.urlencoded({ extended: true }));

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/public/index.html");
});

app.post("/submit", (req, res) => {
  console.log(req.body);
  const { email, name, password } = req.body;

  fs.appendFile("file.txt", `${email},${name},${password}\n`, (err) => {
    if (err) {
      console.error(err);
      return res.status(500).json({ status: "error", message: "文件保存失败" });
    }
    console.log("file saved success");
    // 返回JSON格式的成功响应,Postman可直接捕获
    res.json({ status: "success", message: "提交成功", data: req.body });
  });
});

app.listen(3000, () => {
  console.log("port running on 3000");
});

如果前端需要在提交成功后跳转回首页,可以在前端表单提交的JS逻辑中处理,比如使用fetch提交后,在成功回调里执行window.location.href = "/",这样既满足Postman查看响应的需求,也不影响前端跳转体验。

内容的提问来源于stack exchange,提问作者rohith reddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 14:46:24