我正在嘗試實現(xiàn)一個 html 表單,該表單接受輸入并將其發(fā)送到節(jié)點 js 服務器,但 html 表單沒有向節(jié)點 js 發(fā)送任何數(shù)據(jù)。它發(fā)出請求但沒有發(fā)送表單數(shù)據(jù)。我有一個 index.html 文件<!DOCTYPE html><html><head><meta charset="utf-8" /><title>Input Form</title></head><body> <h1>Send a message:</h1> <form action="http://localhost:3000/action" method="POST"> <label for="data">Message:</label> <input type="text" id="data" placeholder="Enter your message" name="text"/> <input type="submit" value="Send message" /> </form></body></html>和一個節(jié)點js文件//Modulesconst fs = require ('fs');const express = require('express');const app = express();const bodyParser = require('body-parser');const http = require('http');const actionRoute=require('./routes/action')const server = http.createServer(app)app.use(express.urlencoded({ extended: true }))app.use(bodyParser.json())app.all('/',(req,res)=>{ res.statusCode = 200; res.setHeader('Content-Type', 'text/html'); res.end(fs.readFileSync('./public/index.html'))})const hostname = 'localhost';const port = 3000app.post('/action',(req,res)=>{ console.log(req.body) res.statusCode=200; res.end("thnx")})server.listen(port , hostname,function(){ console.log('Server running at http://'+hostname+':'+port);});目錄結構:||-index.js|-public|--index.html在發(fā)布路由中,req.body 為空,它打印{}
HTML 表單不向節(jié)點 js 發(fā)送數(shù)據(jù)
汪汪一只貓
2022-12-18 16:02:59