首页 > 代码库 > nodejs get请求

nodejs get请求

const http = require(‘http‘);

http.get(‘http://192.168.1.6:8080/getDemo?msg=12‘, (res) => {
  const { statusCode } = res;
  const contentType = res.headers[‘content-type‘];

  let error;
  if (statusCode !== 200) {
    error = new Error(`Request Failed.\n` +
                      `Status Code: ${statusCode}`);
  } else if (!/^application\/json/.test(contentType)) {
    error = new Error(`Invalid content-type.\n` +
                      `Expected application/json but received ${contentType}`);
  }
  if (error) {
    console.error(error.message);
    // consume response data to free up memory
    res.resume();
    return;
  }

  res.setEncoding(‘utf8‘);
  let rawData = ‘‘;
  res.on(‘data‘, (chunk) => { rawData += chunk; });
  res.on(‘end‘, () => {
    try {
      const parsedData = JSON.parse(rawData);
      console.log(parsedData);
    } catch (e) {
      console.error(e.message);
    }
  });
}).on(‘error‘, (e) => {
  console.error(`Got error: ${e.message}`);
});

 

nodejs get请求