首页 > 解决方案 > 如何使用 ExpressJS 发出查询请求

问题描述

我应该在 ExpressJS 文件中写入什么命令,以便公开单个 HTTP 端点 (/api/search?symbol=$symbol&period=$period)

在职的

    app.get('/api/search/', (req, res) => {
     res.send(req.query)
 })

不工作:

    app.get('/api/search?symbol=$symbol&period=$period', (req, res) => {
    res.send(req.query)
})

标签: javascriptnode.jsexpress

解决方案


app.get('/api/search?symbol=$symbol&period=$period', (req, res) => {
    res.send(req.query)
})

代替它,您必须编写以下代码

const note = require('../app/controllers/note.controller.js');
  // Create a new API CALL
    app.get('/comment/get', note.index); // In socket.controller.js i have function with the name of index 


//note.controller.js file code
exports.index = (req, res) => {
    var requestTime = moment().unix();

    req.body = req.query;

console.log(req.body); // you will able to get all parameter of GET request in it.

}

让我知道如果我需要解释更多关于

对于 API 的 express 示例代码,您可以查看此...

https://github.com/pawansgi92/node-express-rest-api-sample


推荐阅读