我想将我的查询传递给我的服务器,然后表达使用该查询进行渲染,例如“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
答案 0 :(得分:0)
将$http.get
更改为以下语法,我认为你很高兴:
getProducts (query) {
return this.$http.get(`myApp/api/items`, params: {q: query})
.then((response) => response.data)
}