log(req.params.contestId) 没有给出任何输出"/>
console.log(req.params.contestId) 没有给出任何输出
我正在尝试从教程中复制代码,并尝试将输出作为 http://localhost:8080/api/contests/contestId 上的一场比赛,但 console.log() 值本身在第一阶段提供任何输出。如何解决?
这是我的 api-Router.ts
import express from "express";
import { connectClient } from "./db";
//a dedicated router object is created
const router = express.Router();
router.get("/contests",async (req,res)=>{
const client = await connectClient();
const contests = await client.collection("contests")
.find()
.project({
id:1,
contestName:1,
categoryName:1,
_id:0,
})
.toArray();
//get the data from mongodb
res.send({contests});
});
router.get("/contest/:contestId", async (req,res)=>{
// console.info(req.params.contestId)
const client = await connectClient();
const contest = await client.collection("contests")
.findOne({id : req.params.contestId})
res.send({contest});
});
export default router;
这就是我访问路由器的方式。
import express from "express";
import os from "node:os";
import config from "./config";
import apiRouter from "./api-router";
import serverRender from "./render";
const server = express();
//middleware to serve stattic assets
server.use(express.static("dist"));
//to make server understand that we are using ejs templating language
server.set("view engine","ejs");
//way to define routes
server.use("/api",apiRouter);
//middleware to respond with data
server.get("/",async (req,res)=>{
const {initialMarkup, initialData} = await serverRender();
res.render("index",{
initialMarkup,
initialData,
})
});
//creating express server by giving PORT & HOST
server.listen(config.PORT,config.HOST, ()=> {
console.info(
`Express Server is listening at ${config.SERVER_URL}`,
`Free Mem:${ os.freemem()/1024/1024}`,
)
}
);
我期待在控制台中打印比赛的 ID。
回答如下:更多推荐
console.log(req.params.contestId) 没有给出任何输出
发布评论