https GET适用于基本节点应用程序,但不适用于create-react-app

时间:2017-05-09 19:53:45

标签: node.js reactjs https get create-react-app

所以我已经获得了一个控制台应用程序来向我的https服务器发出成功请求:

console.log('Initializing...')

const https = require('https')

process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'

const options = {
    port: '3001',
    hostname: '127.0.0.1',
    method: 'GET',
    path: '/data',
    headers: {
        'Authorization': 'Basic correct'
    }
}

const req = https.request(options, (response) => {
    response.on('data', (d) => {
        console.log(d.toString())
    })
})

req.on('error', (e) => {
    console.log('error: ' + e.message)
})

req.end()

但是,当我尝试将其转移到使用 create-react-app 制作的项目时:

import React from 'react'
import ReactDOM from 'react-dom'
import https from 'https'

import App from './App'

ReactDOM.render(
  <App />,
  document.getElementById('root')
)


document.getElementById('p').textContent = 'test'


process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'

const options = {
    port: '3001',
    hostname: '127.0.0.1',
    method: 'GET',
    path: '/data',
    headers: {
        'Authorization': 'Basic correct'
    }
}

const req = https.request(options, (response) => {
    response.on('data', (d) => {
        document.getElementById('p').textContent = d.toString()
    })
})

req.on('error', (e) => {
    document.getElementById('p').textContent = e.message
})

req.end()

我得到&#39; XHR错误&#39;。我不确定该怎么做,我尝试了一些不同的东西(比如在客户端使用fetch),但看起来我需要一些帮助。

旁注:服务器没有接收请求,更不用说响应(与其他请求一样,一切都成功)

(我使用https,因为我喜欢服务器端提供的配置选项,我认为在两个位置使用它都很方便,而不是选择fetch或xhr)

感谢任何回复,谢谢:)

以下是评论中人员的服务器代码:

const fs = require('fs')
const http = require('http')
const https = require('https')

const express = require('express')
var app = express()

var port = 3001


var printReqReceived = function(request) {
    console.log('received request for ' + request + ', attempting to authorize...')
}

var printAuthenticated = function(authenticated = true, willRespond = true) {
    if(!authenticated)
        console.log('Authorization attempt revoked')
    else if(willRespond)
        console.log('Authorization attempt accepted, preparing response...')
    else
        console.log('Authorization attempt accepted')
}

var responseSent = function() {
    console.log('Response sent')
}

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*")
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
  next()
})

app.get('/bundles', function(request, response) {

})

app.get('/data', function(request, response) {
    printReqReceived('data')

    var auth = request.get('Authorization')
    if(auth.substr(auth.indexOf(' ') + 1, auth.length) == 'correct')  {

        printAuthenticated()

        response.writeHead(200, {
            'Content-Type': 'application/javascript',
            'Content-Disposition': 'inline; filename=App.js'
        })
       fs.createReadStream('./build/static/js/main.4e81bda7.js').pipe(response)

        responseSent()

    } else {
        printAuthenticated(false)
        response.send('Authorization attempt revoked') 
    }
})


var options = {
    key: fs.readFileSync('./privateKey.pem'),
    cert: fs.readFileSync('./cert.pem'),
    requestCert: false,
    rejectUnauthorized: false

}

https.createServer(options, app).listen(port, function() {
    console.log('Server is now listening on port ' + port + '!')
})

我意识到这个设置有点时髦,我只是想让请求正常运行 - 这就是全部。

如果它不能与create-react-app一起使用,我是否必须使用whatwg-fetch? (对我来说似乎很奇怪,因为他们都在做同样的事情而且最重要的是,我试过whatwg-fetch无济于事)

0 个答案:

没有答案