简体   繁体   English

如何将查询参数从我的前端服务传递到我的服务器?

[英]How can I pass a query param from my service on the front end to my server?

I'm trying to get the user data back from my database, but I can't seem to figure out how to send a query param to my server in order to do such a thing.我正在尝试从我的数据库中取回用户数据,但我似乎无法弄清楚如何将查询参数发送到我的服务器以执行此操作。 My angular front end is running on a different port than my server (not sure that matters) but I can't figure it out.我的 angular 前端运行在与我的服务器不同的端口上(不确定是否重要),但我无法弄清楚。

I've tried using http.get to call the backend, but that doesn't seem to work.我试过使用 http.get 来调用后端,但这似乎不起作用。

This is the code in my auth.service:这是我的 auth.service 中的代码:

reload() {
    this.http.get('/user', {params: {userId: 'test'}});
}

This is the code on my server这是我服务器上的代码

app.get("/user/?userId", (req, res, next) => {
  console.log('hello  2ddsaczczxczxC')
    console.log(req);
})

My console logs aren't logging anything so it's clearly not reaching the backend at all.我的控制台日志没有记录任何内容,因此它显然根本没有到达后端。

Here is the change required for accessing any as such request parameters.这是访问任何此类请求参数所需的更改。 Update your rest end point to将您的 rest 端点更新为

/user/:userid

app.get("/user/:userId", (req, res, next) => {
  console.log('hello  2ddsaczczxczxC')
  console.log(req.params);
})

Then access the query using either by request.params or req.query please check the documentation of expressjs as well.然后使用 request.params 或 req.query 访问查询,请同时查看 expressjs 的文档。

You can try this:你可以试试这个:

app.get("/user/:userId", (req, res, next) => {
   console.log();
}

You can pass query parameter by doing this您可以通过这样做传递查询参数

 reload() {
    this.http.get('/user', {queryParams: {userId: 'test'}});
}

暂无
暂无

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

相关问题 如何从节点/ Express服务器提供Vuejs前端文件? - How can I serve my Vuejs front end files from my node/express server? 如何使前端应用程序访问节点服务器api - How can I make my front end app access my node server apis 如何使用 Node.js 服务器将我的图像数据从前端保存到 Mongoose 数据库 - How can I save my image data from front end to Mongoose database using Node.js server 如何将 Heroku 生成的服务器端口应用到我的前端 Vue.js 应用程序? - How can I apply the Heroku generated server port to my front-end Vue.js app? 我怎样才能等待我的“请求”和响应回到前端? - How can I wait for my "request" and response back to front end? Koa server--如何在中间件之间传递服务器中的数据,以便我可以将它传递给前端 - Koa server-- How to pass data in the server between middlewares so I can pass it to the front end 尝试从前端加载.tsv文件。 该文件保留在我的服务器上。 如何使用Express做到这一点? - Trying to load a .tsv file from the front end. The file stays on my server. How do I do this using Express? 我正在尝试使用 angular 将我的后端付款连接到我的前端卡费用。 我怎样才能做到这一点? - I'm trying to connect my back end payment to my front end card charge with angular. How can I achieve this? 如何将新的 React JS 前端嵌入到我现有的单体 Node JS 应用程序中? - How can I embed new React JS Front-end in my existing monolithic Node JS App? 如何在我从后端发送的前端返回 json 响应? - How to get the json response back in the front end that I am sending from my backend?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM