node express使用http获取接口数据

const express = require('express')
const router = express.Router()
const http = require('http')

/* GET home page. */
router.get('/', function(req, res, next) {
  let body = ''
  http.get(url='http://127.0.0.1:3000/setdata', (response) => {
    response.on('data', (data) => {
      body += data
    }).on('end', () => {
      res_data = JSON.parse(body)
      console.log(res_data.data)
    }).on('error', (error) => {
      console.log(error)
    })
  })
  res.render('index', { title: 'Express' })
})

module.exports = router

 

你可能感兴趣的:(js)