console.log(req.params.contestId) 没有给出任何输出

编程入门 行业动态 更新时间:2024-10-04 11:20:14

console.<a href=https://www.elefans.com/category/jswz/34/1771007.html style=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) 没有给出任何输出

本文发布于:2024-05-31 00:47:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771098.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:log   console   req   contestId   params

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!