Node.js - 不能 render/send 来自中间件的东西
Node.js - Can't render/send something from middleware
我在端口 8082 上有一个快速服务器 运行nnig。在访问路由“/”作为 GET 后,它呈现 index.html 文件,然后我将其形式发送到“/”作为POST 使用 app.js 脚本。此路由有一个名为 "validate1" 的中间件,它检查 header "x-client" 是否等于 "student" 以控制此路由访问。
问题是当 header "x-client" 不等于 "student" 并且 "validate1" 中间件最终出现在 "else" 中时,我只是无法呈现或发送任何内容作为响应。 node.js 打印我的 "here" 但它不会 运行 它的下一个 "res.send()" 行。
在“/travel”路线上,我有几乎相同的中间件完美运行,但我使用的是 GET 方法和 none jquery.
更新:由于 "app.js" 中 jquery POST 函数中的“.then()”,我发现我的响应正在浏览器控制台中打印,但即使删除它,响应永远不会显示在页面上。
index.html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge"><!-- rota no css-->
<title>Document</title>
<link rel="stylesheet" href="./style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<form method="POST">
Nome:<input name="nome" type="text" id="nome">
<br>
Sobrenome:<input name="sobrenome" type="text" id="sobrenome">
<button>Send data</button>
</form>
</body>
<script src="./app.js"></script>
</html>
app.js
$("button").on("click", (event) => {
$("button").attr("disabled","disabled")
const nome = $("#nome").val().trim()
const sobrenome = $("#sobrenome").val().trim()
const user = {
nome,
sobrenome
}
$.ajax({
url:"/",
method:"POST",
data:user,
headers:{
"x-client":"student",
"x-class":500
}
}).then((resposta) => {
console.log(resposta)
}).catch((erro) => {
console.log("Error:" + erro)
})
})
routing.js
const express = require("express")
const path = require("path")entre windows e mac
const router = express.Router()
router.get("/",(req,res) => {
res.sendFile(path.join(__dirname,"../views/index.html"))
})
//not working route
const validate1 = (req,res,next) => {
const access = req.headers["x-client"]
if(access === "student2"){
next()
}
else{
console.log("here")//it runs!
res.send("You don't have access to it!")//it doesn't run!
}
}
router.post("/", validate1 ,(req,res) => {
const user = {
nome:req.body.nome,
sobrenome: req.body.sobrenome
}
res.json(user)
})
//working route
const validate2 = (req,res,next) => {
const access = false
if(access){
next()
}
else{
res.send("You don't have access to it!")
}
}
router.get("/travel",validate2,(req,res) => {
res.send("Travel!")
})
实际上我没有收到任何错误,而是收到了一条消息,我认为我的表单按钮刚刚被禁用但没有任何反应。
提前致谢。
我不确定,但也许 post 期望 json
所以尝试发送
res.send({msg:'some msg'});
或将 content-type header 设置为 'text'
您正在进行 POST
HTTP 调用,而您在 routing.js
文件中使用 router.get
。
你能把你的代码改成这样吗:
router.post("/", validate1 ,(req,res) => {
const user = {
nome:req.body.nome,
sobrenome: req.body.sobrenome
}
res.json(user)
})
对 app.js 进行以下更改以在 index.html
中显示响应消息
$("button").on("click", (event) => {
$("button").attr("disabled","disabled")
const nome = $("#nome").val().trim()
const sobrenome = $("#sobrenome").val().trim()
const user = {
nome,
sobrenome
}
$.ajax({
url:"/",
method:"POST",
data:user,
headers:{
"x-client":"student",
"x-class":500
}
}).then((resposta) => {
console.log(resposta)
$('body').append(resposta);
}).catch((erro) => {
console.log("Error:" + erro)
})
})
我相信你的页面很清爽?由于表单元素本身可能正在执行提交,因此您可以尝试在点击处理程序的顶部包含 event.preventDefault()
。这可以解释为什么您的服务器的 else 子句是 运行,但没有看到任何消息 client-side。
我在端口 8082 上有一个快速服务器 运行nnig。在访问路由“/”作为 GET 后,它呈现 index.html 文件,然后我将其形式发送到“/”作为POST 使用 app.js 脚本。此路由有一个名为 "validate1" 的中间件,它检查 header "x-client" 是否等于 "student" 以控制此路由访问。
问题是当 header "x-client" 不等于 "student" 并且 "validate1" 中间件最终出现在 "else" 中时,我只是无法呈现或发送任何内容作为响应。 node.js 打印我的 "here" 但它不会 运行 它的下一个 "res.send()" 行。
在“/travel”路线上,我有几乎相同的中间件完美运行,但我使用的是 GET 方法和 none jquery.
更新:由于 "app.js" 中 jquery POST 函数中的“.then()”,我发现我的响应正在浏览器控制台中打印,但即使删除它,响应永远不会显示在页面上。
index.html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge"><!-- rota no css-->
<title>Document</title>
<link rel="stylesheet" href="./style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<form method="POST">
Nome:<input name="nome" type="text" id="nome">
<br>
Sobrenome:<input name="sobrenome" type="text" id="sobrenome">
<button>Send data</button>
</form>
</body>
<script src="./app.js"></script>
</html>
app.js
$("button").on("click", (event) => {
$("button").attr("disabled","disabled")
const nome = $("#nome").val().trim()
const sobrenome = $("#sobrenome").val().trim()
const user = {
nome,
sobrenome
}
$.ajax({
url:"/",
method:"POST",
data:user,
headers:{
"x-client":"student",
"x-class":500
}
}).then((resposta) => {
console.log(resposta)
}).catch((erro) => {
console.log("Error:" + erro)
})
})
routing.js
const express = require("express")
const path = require("path")entre windows e mac
const router = express.Router()
router.get("/",(req,res) => {
res.sendFile(path.join(__dirname,"../views/index.html"))
})
//not working route
const validate1 = (req,res,next) => {
const access = req.headers["x-client"]
if(access === "student2"){
next()
}
else{
console.log("here")//it runs!
res.send("You don't have access to it!")//it doesn't run!
}
}
router.post("/", validate1 ,(req,res) => {
const user = {
nome:req.body.nome,
sobrenome: req.body.sobrenome
}
res.json(user)
})
//working route
const validate2 = (req,res,next) => {
const access = false
if(access){
next()
}
else{
res.send("You don't have access to it!")
}
}
router.get("/travel",validate2,(req,res) => {
res.send("Travel!")
})
实际上我没有收到任何错误,而是收到了一条消息,我认为我的表单按钮刚刚被禁用但没有任何反应。
提前致谢。
我不确定,但也许 post 期望 json 所以尝试发送
res.send({msg:'some msg'});
或将 content-type header 设置为 'text'
您正在进行 POST
HTTP 调用,而您在 routing.js
文件中使用 router.get
。
你能把你的代码改成这样吗:
router.post("/", validate1 ,(req,res) => {
const user = {
nome:req.body.nome,
sobrenome: req.body.sobrenome
}
res.json(user)
})
对 app.js 进行以下更改以在 index.html
中显示响应消息$("button").on("click", (event) => {
$("button").attr("disabled","disabled")
const nome = $("#nome").val().trim()
const sobrenome = $("#sobrenome").val().trim()
const user = {
nome,
sobrenome
}
$.ajax({
url:"/",
method:"POST",
data:user,
headers:{
"x-client":"student",
"x-class":500
}
}).then((resposta) => {
console.log(resposta)
$('body').append(resposta);
}).catch((erro) => {
console.log("Error:" + erro)
})
})
我相信你的页面很清爽?由于表单元素本身可能正在执行提交,因此您可以尝试在点击处理程序的顶部包含 event.preventDefault()
。这可以解释为什么您的服务器的 else 子句是 运行,但没有看到任何消息 client-side。