前言:使用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()
})