如何使express从param接收查询

问题描述 投票:0回答:1

我想将我的查询传递给我的服务器然后表达使用该查询进行渲染,例如“localhost:3000 / myApp / items?search = myQuery这是与angularjs,express和node。

angular.service

 export class ProductsService {
      constructor ($http) {
        'ngInject'
        this.$http = $http;
      }

      getProducts (query) {
        return this.$http.get(`myApp/api/items?q=${query}`).then((response) => response.data)
      }
    }

这是我在快递中的路线

const express = require('express')
const router = express.Router()
const product = require('./product-controller')

router.get('/items/q=:query', product.getProducts)

module.exports = router

调节器

const ProductService = require('./product-service')

class ProductController {

  static async getProducts (req, res) {
    const query = req.params.query
    console.log(query)
    try {
      const ProductServiceInstance = new ProductService(req, res)
      const products = await ProductServiceInstance.getProducts(query)
      return res.render.json(products);
    }
    catch (err) {
      throw(err)
    }
  }

服务

const axios = require('axios')

    class ProductService {

      async getProducts (query) {
        try {
          const url = 'https://api.zysn.com/sites/zys/search?q=' + query
          const response = await axios.get(url)
          return response.data.results
        }
        catch (err) {
          throw(err)
        }
      }

    }

    module.exports = ProductService
angularjs express
1个回答
0
投票

$http.get更改为以下语法,我认为你很高兴:

getProducts (query) {
        return this.$http.get(`myApp/api/items`, params: {q: query})
                   .then((response) => response.data)
}
© www.soinside.com 2019 - 2024. All rights reserved.