【问题标题】:Retrieving data from server based on the data sent from client to server根据从客户端发送到服务器的数据从服务器检索数据
【发布时间】:2021-06-04 11:17:38
【问题描述】:

我想从客户端发送一个 id(string) 到服务器端,并基于该 id,我想从服务器端检索与该 id 对应的其他信息。我正在使用 Node.js 作为后端。
另外,我尝试在 Internet 上搜索此内容,但找不到解决方案。希望这不是一个愚蠢的/已经提出的问题。

【问题讨论】:

    标签: javascript node.js ajax post get


    【解决方案1】:

    使用 Vanilla JS fetch api 可以将内容从前端发送到后端并获得响应。

    这是一个例子:

    // Here, I'm making a request to an api with the following url:
    const URL = 'https://jsonplaceholder.typicode.com/todos'
    const ID = 1
    
    fetch(`${URL}/${ID}`)
      .then(res => res.json()) // parse the received information into json
      .then(json => console.log(json)) // print the json

    fetch api 正在向 url 发出请求并接收到相应的响应。您可以查看 MDN 文档了解更多详细信息:https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API


    假设您使用 NodeJS + Express 作为后端,您必须设置某种路由。这是一个非常简单的快速服务器示例。

    // bring express into your server file
    const express = require('express')
    
    // if your front and back end run on different servers, you might need
    // to worry about CORS, so you can also bring that in.
    const cors = require('cors')
    
    // initialize your application
    const app = express()
    
    // take care of cors problem by adding middleware
    app.use(cors())
    
    // let's say you have some user data in an array:
    const users = [
        {
            id: 1,
            name: "John"
        },
        {
            id: 2,
            name: "Bill"
        }
    ]
    
    // create your routes.  I'll just make one as an example.
    // This route expects to receive a parameter as an id.  The URL
    // would look like this: http://yourwebsite.com/123, where 123 is
    // the id.
    app.get('/:id', (req, res) => {
        // get the id from the req.params object
        const { id } = req.params
    
        // do whatever you want with id. maybe fetch data from database
        // by id.  Here, we can just take data from users array.
        // We have to turn id into a Number, since params are strings 
        // by default.
        const foundUser = users.find(user => user.id === Number(id))
    
        // then we can return the user
        res.send(foundUser)
    })
    
    // finally, make sure your app is listening for incoming requests 
    // on some port.  I'll just use port 3000 and make the server 
    // console log a message if there are no errors.
    app.listen(3000, console.log('Server listening on port 3000'))
    

    如果您正在讨论如何从服务器向另一台服务器发出请求,您可以使用 npm 包,如 axios 或 node-fetch。

    const fetch = require('node-fetch')
    
    // then you can make a fetch request the same way it was done on the 
    // front end
    
    fetch('https://github.com/')
        .then(res => res.text())
        .then(body => console.log(body));
    

    话虽如此,我强烈建议你去 youtube 或查看官方 NodeJS/Express 文档,这样你就可以真正了解需要做什么。

    【讨论】:

    • 谢谢,这就是我要找的。能否补充一下我们如何在后端处理此类编码请求?
    • 刚刚添加了后端。
    • 谢谢!在问这些问题之前,我肯定会做更多的研究。
    猜你喜欢
    • 2022-01-12
    • 2013-04-11
    • 2021-12-07
    • 2014-09-07
    • 2012-08-29
    • 2020-08-17
    • 2015-03-05
    • 2021-01-26
    • 1970-01-01
    相关资源
    最近更新 更多