我在管理 xmlhttprequest post 請求時遇到問題。這是節(jié)點快遞服務(wù)器的代碼:const fs = require("fs")const path = require("path")const express = require("express")const app = express()const port = 3001app.use(express.static(__dirname))app.use("/", (request, response) => { console.log("inside app.use") response.sendFile(path.join(__dirname, "index.html"))})app.post("/database", (request, response) => { console.log("inside app.use02") console.log("request-body: "+request) console.log("response-body: "+response) response.send("it works")})app.listen(port)問題是,當我對 /database url 發(fā)出 ajax 請求時,它由 app.use 語句而不是 app.post 語句提供服務(wù)。這是為什么?我不明白expressjs是如何工作的,它是什么?const btnForm = document.getElementById("form-btn")const input01 = document.getElementById("firstName")const input02 = document.getElementById("lastName")const input03 = document.getElementById("profession")const form = document.getElementById("form01")form.addEventListener("submit", sendForm)const httprequest = new XMLHttpRequest()const FD = new FormData()function sendForm(event){ event.preventDefault() console.log("sendForm") FD.append(input01.name, input01.value) FD.append(input02.name, input02.value) FD.append(input03.name, input03.value) httprequest.open("POST", "http://localhost:3001/database") httprequest.send(FD) }我想知道的是為什么 ajax 請求首先由 app.use 語句而不是 app.post 語句提供服務(wù),我認為既然我正在執(zhí)行 ajax post 請求,它應(yīng)該得到 app.post 的響應(yīng)聲明,鄙視他之前調(diào)用的 app.use 聲明。
回調(diào)的優(yōu)先級,如何使用 Node Express JS 處理 xmlhttprequest
慕的地6264312
2023-07-06 10:12:39