nodejs发起http或https请求

前言:使用node内置模块http、https

http请求

const express = require('express')
const http = require('http')

const app = express()

const loginConfig = (token) => {
  return {
    hostname: 'api.test.com',
    port: 80,
    path: `/test?access_token=${token}`,
    method: 'GET'
  }
}

app.get('/login', (req, res) => {
  const options = loginConfig(req.query.token)
  const http_req = http.request(options, (result) => {
    let data = ''
    result.on('data', (chunk) => {
      data += chunk
    })
    result.on('end', () => {
      data = JSON.parse(data)
      res.send({ code: 200, msg: 'success', data })
    })
  })
  http_req.end()
})

https请求

const express = require('express')
const https = require('https')

const app = express()

const loginConfig = (token) => {
  return {
    hostname: 'api.weixin.qq.com',
    port: 443,
    path: `/wxa/checksession?access_token=${token}`,
    method: 'GET'
  }
}

app.get('/wx_login', (req, res) => {
  const options = loginConfig(req.query.token)
  const https_req = https.request(options, (result) => {
    let data = ''
    result.on('data', (chunk) => {
      data += chunk
    })
    result.on('end', () => {
      data = JSON.parse(data)
      res.send({ code: 200, msg: 'success', data })
    })
  })
  https_req.end()
})

相关推荐

  1. nodejs发起httphttps请求

    2023-12-09 05:34:10       58 阅读
  2. C++发起Https请求

    2023-12-09 05:34:10       47 阅读
  3. RestTemplate发送https请求

    2023-12-09 05:34:10       54 阅读
  4. Linux发送HTTP请求

    2023-12-09 05:34:10       33 阅读
  5. go进行http,getpostJson请求

    2023-12-09 05:34:10       55 阅读
  6. linux发送http请求命令

    2023-12-09 05:34:10       57 阅读
  7. QT-发送HTTP请求/QNetworkAccessManager

    2023-12-09 05:34:10       60 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-09 05:34:10       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-09 05:34:10       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-09 05:34:10       82 阅读
  4. Python语言-面向对象

    2023-12-09 05:34:10       91 阅读

热门阅读

  1. Swagger提示请确保swagger资源接口正确

    2023-12-09 05:34:10       61 阅读
  2. http和https区别

    2023-12-09 05:34:10       58 阅读
  3. TCP通讯

    TCP通讯

    2023-12-09 05:34:10      52 阅读
  4. Golang优雅实现按比例切分流量

    2023-12-09 05:34:10       55 阅读
  5. 数据库基础概念与范式反范式总结

    2023-12-09 05:34:10       53 阅读
  6. C++基础

    C++基础

    2023-12-09 05:34:10      43 阅读
  7. 初识主力投资者

    2023-12-09 05:34:10       54 阅读