我正在努力解决一些Express路由,它们的行为不同,即使要执行的代码是相同的.
我希望客户端应用程序按如下方式调用该API:
async search(){
const query = this.$refs.searchInput.value;
console.log(query);
const addressToFetch = "/api/budget/search?q=" + query;
await fetch(addressToFetch)
.then((response) => response.json())
.then((response) => {
console.log(response);
});
}
其中,值query
取自以下输入的HTML标记:
<div class="input-group row text-right">
<!--flex-nowrap-->
<div class="col">
<input
type="text"
id="searchInput"
name="search"
placeholder="Search"
ref="searchInput"
/>
<button
type="button"
class="btn btn-primary btn-search btn-filter"
@click="search"
>
<i class="fa fa-search" aria-hidden="true"></i>
</button>
<button
type="button"
class="btn btn-danger"
v-if="filteredResults"
@click="getExpenses"
>
Undo
</button>
</div>
</div>
服务器端应用程序按如下方式处理请求:
app.get("/api/budget/search", verify, async (req, res) => {
console.log(req.query.q);
const users = await db
.collection("users")
.find({ username: { $regex: req.query.q } })
.toArray();
users.forEach((user) => {
delete user.password;
});
res.json(users);
});
身份验证后,该函数返回[]
,甚至不打印服务器端日志(log).与postman 核对,我可以看到,无论如何,回复是200OK.出于测试目的,我创建了一条不同的路径,其中包含相同的代码.事实证明,新的路由产生了不同的(好的)结果:
app.get("/api/users/search", verify, async (req, res) => {
console.log(req.query.q);
const users = await db
.collection("users")
.find({ username: { $regex: req.query.q } })
.toArray();
users.forEach((user) => {
delete user.password;
});
res.json(users);
});
我把所有的路由都留给你,看看有没有什么问题:
const express = require("express");
const fs = require("fs/promises");
const session = require("express-session");
const { MongoClient } = require("mongodb");
const exp = require("constants");
const uri = "mongodb://mongohost";
const app = express();
const client = new MongoClient(uri);
let db = null;
app.use(express.static(`${__dirname}/public`));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(
session({
secret: "segreto",
resave: false,
})
);
function verify(req, res, next) {
if (req.session.user) {
next();
} else {
res.status(403);
}
}
app.post("/api/auth/signup", async (req, res) => {});
app.post("/api/auth/signin", async (req, res) => {...});
app.get("/api/budget/whoami", verify, (req, res) => {...});
app.get("/api/budget", verify, async (req, res) => {...});
app.get("/api/budget/:year", verify, async (req, res) => {...});
app.get("/api/budget/:year/:month", verify, async (req, res) => {...});
app.get("/api/budget/:year/:month/:id", verify, async (req, res) => {...});
app.post("/api/budget/:year/:month", verify, async (req, res) => {...});
app.put("/api/budget/:year/:month/:id", verify, async (req, res) => {...});
app.delete("/api/budget/:year/:month/:id", verify, async (req, res) => {...});
app.get("/api/balance", verify, async (req, res) => {...});
app.get("/api/balance/:id", verify, async (req, res) => {...});
app.get("/api/budget/search", verify, async (req, res) => {...});
app.get("/api/users/search", verify, async (req, res) => {...});
app.listen(3000, async () => {
await client.connect();
db = client.db("balanceApp");
});
我不明白第一条路由的问题,我能做些什么来解决这个问题吗?非常感谢您的耐心等待.