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

点击提交按钮后Node.js无响应,POST请求无法触发求助

Fixing Your Node.js POST Route Trigger Issue

Hey there! Let's break down why your submit button isn't hitting the /admin POST route and fix each problem step by step.

1. Frontend (home.html) Issues

Your form isn't set up properly to send a POST request to your backend:

  • The submit button uses type="button" instead of type="submit" — plain buttons don't trigger form submission by default.
  • The form lacks method="POST" and action="/admin" attributes, so the browser doesn't know where or how to send the data.
  • The <select> element has no name attribute, meaning the selected value won't be included in the form data sent to the server.
  • There's an extra, unused </a> tag that should be removed.

Modified home.html

<body>
  <div class="loginBox">
    <img src="user.png" class="user">
    <h2>Docker</h2>
    <form id="tableForm" method="POST" action="/admin">
      <select name="dockerCommand">
        <optgroup label="Select docker commands here">
          <option value="0"></option>
          <option value="1">docker start</option>
          <option value="2">docker build</option>
          <option value="3">docker run</option>
          <option value="4">docker pull</option>
          <option value="5">docker stop</option>
        </optgroup>
      </select><br>
      <input type="submit" value="submit" />
    </form>
  </div>
</body>

2. Backend (server.js) Issues

Your Node.js server has several critical issues preventing the POST route from working correctly:

  • Missing request body parsing middleware: Express doesn't automatically parse form data from POST requests. You need to add middleware to handle this.
  • Invalid e.preventDefault(): This is a frontend JavaScript method used to stop default browser actions — it doesn't belong in your backend route handler, and will throw an error since e isn't defined here.
  • Incomplete session validation: After sending the "Unauthorized access" response, you don't stop the function execution, so the server will still try to send the HTML file.
  • Incorrect file path in sendFile: The path '/.html' is invalid; you need to specify the actual filename (e.g., home.html).

Modified server.js

// First, make sure you have these required imports at the top
const express = require('express');
const session = require('express-session');
const app = express();

// Add session middleware (you probably already have this, but just in case)
app.use(session({
  secret: 'your-secret-key-here', // Replace with a real secret for production
  resave: false,
  saveUninitialized: true
}));

// Add middleware to parse form data from POST requests
app.use(express.urlencoded({ extended: true }));

app.get('/admin', function(req, resp) {
  const session = req.session;
  if (session.uniqueID !== 'admin') {
    return resp.send('Unauthorized access'); // Add return to stop further execution
  }
  resp.sendFile('home.html', { root: __dirname }); // Correct the file path
});

app.post('/admin', function(req, resp) {
  resp.setHeader('Content-Type', 'application/json');
  console.log('Admin POST route triggered!');
  console.log('Selected Docker command:', req.body.dockerCommand); // Access the submitted form data
  resp.json({ success: true, command: req.body.dockerCommand }); // Send a JSON response back to the client
});

// Don't forget to start your server
app.listen(3000, () => {
  console.log('Server running on port 3000');
});

3. How to Test

  1. Install required dependencies if you haven't already: run npm install express express-session
  2. Start your server with node server.js
  3. Visit http://localhost:3000/admin (ensure your session's uniqueID is set to 'admin', or temporarily comment out the session check to test the flow)
  4. Select a Docker command from the dropdown and click Submit — you should see the "Admin POST route triggered!" message in your server console, and the browser will receive a JSON response confirming the submission.

内容的提问来源于stack exchange,提问作者mani R

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:39:52