繁体   English   中英

如何在 Express.js 中使用查询字符串参数编写 API 路由

[英]How to write an API route with query string parameters in Express.js

尝试编写一个简单的路由,通过它的 id 属性/列获取记录

const router = require("express").Router();    
router.get("/record/:id", getRecordById)    // CHANGE HERE

这就是我能够用于前端 ajax 的方式 -

http://localhost:3001/record/1

我需要改变什么,才能将路线用作

http://localhost:3001/?record=1

您应该创建这样的路线。

router.get("/", function (req, res) {
  // Get record id
  const record_id = req.query.record;
});

在你的情况下

API: router.get("/record", getRecordById)

URL: http://localhost:3001/record?id=1

获取api中的查询字符串:req.query.id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM