简体   繁体   English

在NODE中同时执行G​​ET和POST请求

[英]GET and POST request at the same time in NODE

I have a search page and I need to send an array which tell me that what is user looking for like: 我有一个搜索页面,我需要发送一个数组,告诉我用户在寻找什么,例如:
{ Model: 'Jeans', Color: 'BLUE} {模特:“牛仔裤”,颜色:“蓝色”

and I should have a GET request to receive data from database. 我应该有一个GET请求,以从数据库接收数据。

Can I have both Get and post request at the same time? 我可以同时拥有Get和Post请求吗? Do you suggest to have one function for both or one function for each? 您是否建议同时具有一项功能或一项功能? if latter then How can I pass data between functions in Express.js? 如果是后者,则如何在Express.js中的函数之间传递数据?

 app.route('/alls/:issuename')
    .get(alls.list)
    .post(alls.list2);

Controller 调节器

exports.list = function(req, res) {
    mongoose.model(var1).find({ color: var2 }).exec(function(err, alls){
        res.jsonp(alls);
    }); 
};

exports.list2 = function(req, res) {

    var var1 = req.body.Model;
    var var2 = req.body.Color;

};

This is ow I pass a parameter to GET in $Resource 这是因为我在$ Resource中将参数传递给GET

angular.module('alls').factory('Alls', ['$resource', function($resource) {

return $resource('alls/:issuename', {issuename: '@issuename'},
    {   
        get:{ method: 'GET', isArray:true }

    });
}]);

How Can I pass an Array? 如何传递数组?

I solved it like this 我这样解决了

$Resource $资源

angular.module('alls').factory('Alls', ['$resource', function($resource) {

return $resource('alls/:issuename', {issuename: '@issuename'},
    {

        get:{ method: 'GET',
        query: {
            Model: '',
            Color: ''
        }, isArray:true }

    });
}]);

And then I receive this data in my Controller 然后我在控制器中收到此数据

 exports.list = function(req, res) {

    var var1 = req.query.Model;
    var var2 = req.query.Color;

    mongoose.model(var1).find({ color: var2 }).exec(function(err, alls) {
        res.jsonp(alls);
    });



};

Note: Array name in $resource can be query or anything else BUT you can only receive the passed data by req.query in controller. 注意:$ resource中的数组名称可以是查询或任何其他内容,但是您只能通过控制器中的req.query接收传递的数据。

In route Section I just need get and post is redundant 在路线部分,我只需要获取和发布是多余的

Hope it Helps Someone Else! 希望对其他人有帮助!

http://expressjs.com/guide/routing.html http://expressjs.com/guide/routing.html

There is a special routing method app.all() which is not derived from any HTTP method. 有一个特殊的路由方法app.all() ,它不是从任何HTTP方法派生的。 It is used for loading middleware at a path for all request methods. 它用于在所有请求方法的路径上加载中间件。

In the following example, the handler will be executed for requests to “/secret” whether using GET, POST, PUT, DELETE, or any other HTTP request method supported in the http module . 在以下示例中,无论使用GET,POST,PUT,DELETE还是http模块支持的任何其他HTTP请求方法,都将对“ / secret”请求执行处理程序。

app.all('/secret', function (req, res, next) {
  console.log('Accessing the secret section ...');
  next(); // pass control to the next handler
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM