简体   繁体   中英

what is the difference between http.reqeust and request in node express?

My code is deployed on UBUNTU Nginx server, admin panel is developed in express js while API is node.js, I have following codes in my node express setup -

app.get('/admin', isUserAllowed, function(req, res) {
    var optionsuser = {
        'method': 'GET',
        'url': urlapi + 'totalcount', //same code is working with https://jsonplaceholder.typicode.com/todos/1
        'headers': {
            'Cookie': 'refreshToken=54cdc074eba0604dff****'
        }
    };

    request(optionsuser, function(error, responsed) {
        
        if (error) throw new Error(error);
        
        var responsedatad = JSON.parse(responsed.body);
        res.locals = {
            title: 'Dashboard',
            totaldata: responsedatad
        };
        res.render('Dashboard/index');
    });
});

Following is my Nginx file -

server {
    server_name testdomain.com www.testdomain.com;
    listen PUBLIC_IP_HERE;
    root /home/test/public_html;
    index index.php index.htm index.html;
    
    location /api {
        proxy_pass http: //PUBLIC_IP_HERE:4000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

    }

    location /admin {
        proxy_pass http: //localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

I am getting 504 Gateway Time-out Error. But when I change my Express codes to -

app.get('/admin', isUserAllowed, function(req, res) {

    var request = http.request({
        host: 'testdomain.com',
        path: '/api/totalcount',
        method: 'GET',
        headers: {
            'Cookie': 'refreshToken=54cdc074eba0604dff****',
        }
    }, function(response) {
        // stuff with response data
    });
    request.end();
    res.locals = {
        title: 'Dashboard',
        totaldata: 0
    };
    res.render('Dashboard/index');
});

It is working fine, Can anyone please let me know what's the difference between http.request and request ?

My problem is that I have used request at so many places so can't update it everywhere.

Thanks much!!

Update - When I change

var optionsuser = {
        'method': 'GET',
        'url': urlapi + 'totalcount',
        'headers': {
            'Cookie': 'refreshToken=54cdc074eba0604dff****'
        }
    };

to

var optionsuser = {
        'method': 'GET',
        'url': 'https://jsonplaceholder.typicode.com/todos/1',
        'headers': {
            'Cookie': 'refreshToken=54cdc074eba0604dff****'
        }
    };

Then it is working fine, please help me.

request() as you show here:

 request(optionsuser, function(error, responsed) { ... });

is a third party library from here . It is a higher level library built on top of http.request() and is generally much easier to use than http.request() for most things (requires you to write less code).

http.request() is built into the nodejs http module, but operates at a lower level. With enough additional code, it can do anything you want. But, the request() library has already written much of that code, making your life easier.

For a variety of reasons, the request() library is now deprecated and will likely be maintained for a period of time, but is no longer getting new features and is not recommended for new projects. You can read about that here .

There's a list of alternatives to request() here which are actively being developed. This list of alternatives all support promises (the modern way of handling asynchronous operations). The request() library by itself does not support promises.

My personal favorite from that list of alternatives is got() because I like how the API works and its a feature-rich implementation, but other popular libraries in that list are node-fetch() and axios() .

Here's an example of getting some JSON from a server using got() :

try {
    const result = await got(someURL).json();
    // use result here
} catch(e) {
    // handle error here
    console.log(e);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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