Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js https GET适用于基本节点应用程序,但不适用于create react应用程序_Node.js_Reactjs_Https_Get_Create React App - Fatal编程技术网

Node.js https GET适用于基本节点应用程序,但不适用于create react应用程序

Node.js https GET适用于基本节点应用程序,但不适用于create react应用程序,node.js,reactjs,https,get,create-react-app,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: '

因此,我得到了一个控制台应用程序,可以通过以下命令向我的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()
但是,当我尝试将其转移到使用创建反应应用程序制作的项目时:

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()
我意识到这是一个有点古怪的设置,我只是想让请求工作-仅此而已


如果它不适用于
create react app
,我是否必须使用
whatwg fetch
?(这对我来说似乎很奇怪,因为他们都在做同样的事情,而且,我尝试了
whatwg fetch
,但没有结果)

这是您的服务器代码吗?我不确定
https
包在客户端是否可用。导入时没有出现错误吗?我也很困惑,如果是客户端代码,
https
以及
process.env
之类的东西都不起作用。但是如果它是
节点
你的
文档
将无法工作。这是客户端代码-它与发布的第一个代码部分中的
https
完美配合,那么
create react app
中有什么不同吗?(因为它没有
create react app
https
env
也没有问题)而且没有-我从
导入中没有收到错误。我也发布了服务器代码
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 + '!')
})