The request module for node. js

Source: Internet
Author: User

The request module makes HTTP requests easier. One of the simplest examples:

   1:var request = require (' request ');
   
   3:request (' http://www.google.com ', function (error, response, body) {
   4:   if (!error && response.statuscode = = 200) {
   5:     Console.log (body);
   6:   }
   7:})

Installation:

NPM Install request

The file stream of the picture can be easily obtained by using the pipe method of request.

   1:var request = require (' request '),
   2:     fs = require (' FS ');
   
   4:request (' https://www.google.com.hk/images/srpr/logo3w.png '). Pipe (Fs.createwritestream (' doodle.png '));

Request also supports OAuth signature requests

For more information on how to use it, click here to continue reading: https://github.com/mikeal/request/

Here is a very simple example of how to crawl the hotel query data (get the price ranking of each room in a time period):

   1:var request = require (' request '),
   2:     fs = require (' FS ');
   
   
   5:var Requrl = ' http://hotel.qunar.com/price/detail.jsp?fromDate=2012-08-18&toDate=2012-08-19&cityurl= Shanghai_city&hotelseq=shanghai_city_2856&cn=5 ';
   
   7:request ({Uri:requrl}, function (err, response, body) {
   8:     
   9:     //console.log (Response.statuscode);
  Ten:     //console.log (response);
  11:     
  :     //If the data volume is large, it is necessary to store the returned data according to the date, the hotel ID, and read the file directly if the data is compared.
  :     var filePath = __dirname + '/data/data.js ';
  14:     
  :     if (fs.exists (FilePath)) {
  :         Fs.unlinksync (FilePath);
  
  :         console.log (' Del file ' + FilePath);
  :     }
  20:     
  :     fs.writefile (FilePath, body, ' UTF8 ', function (err) {
  :         if (err) {
  At:             throw err;
  :         }
  
  :         console.log (' Save ' + filePath + ' ok~ ');
  :     });
  28:     
  £ º     console.log (' Fetch ' + requrl + ' ok~ ');
  30:});

This example originates from a friend who does a hotel business and wants to know the competitive price that it offers to its customers on the site:

1, if the price is too low, the money they earn will be less, so if their price is the lowest, you need to see the second low is how much, and then decide whether to adjust;

2, if the price is too high, then the search out of the ranking results compared to the latter, there is no customer to book hotels, business is not

Because of the hotel booking business, such as more than 2000, if a relying on manual to query ranking is more passive, and it is difficult to bigger, so his demand I analyzed a bit is feasible, and can be made a good real-time warning system (of course, the data 5-10 minutes will automatically refresh the page once). This will ensure maximum profit, improve sales, customer department efficiency, speed up the number of hotel cooperation and the company's personnel expansion:

1. No loss, no loss of business;

2, if the price is found to provide too low or too high, need to support the call Platform API interface, directly modify the price;

3, has the automatic Generation Analysis report function, analyzes the competitor in the price adjustment strategy the change situation;

The request module for node. js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.