javascript无法向Eureka服务器注册React应用程序
错误:
Access to fetch at 'http://127.0.0.1:8761/eureka/apps/auth0-react-sample' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
代码:
索引。js(React应用程序)
const PORT = process.env.PORT || 3000;
const eurekaHelper = require('./eureka-helper/eureka-helper');
const Eureka = require('eureka-js-client').Eureka;
// const PORT = 3000;
// const HOST = '0.0.0.0';
// example configuration
const client = new Eureka({
// application instance information
instance: {
app: 'auth0-react-sample',
hostName: 'localhost',
ipAddr: 'localhost',
//statusPageUrl: 'http://localhost:3000/',
port: 3000,
vipAddress: 'auth0-react-sample',
dataCenterInfo: {
name: 'default',
},
registerWithEureka: true,
fetchRegistry: true,
},
eureka: {
// eureka server host / port
host: 'localhost',
port: 8761,
servicePath: '/eureka/apps',
},
});
// Listen on a specific host via the HOST environment variable
var host = process.env.HOST || '0.0.0.0';
// Listen on a specific port via the PORT environment variable
var port = process.env.PORT || 3000;
const onRedirectCallback = (appState) => {
history.push(
appState && appState.returnTo ? appState.returnTo : window.location.pathname
);
};
// Please see https://auth0.github.io/auth0-react/interfaces/auth0provideroptions.html
// for a full list of the available properties on the provider
const config = getConfig();
const providerConfig = {
domain: config.domain,
clientId: config.clientId,
...(config.audience ? { audience: config.audience } : null),
redirectUri: window.location.origin,
onRedirectCallback,
};
client.start(error=>{
console.log(error || 'NodeJS Eureka started');
ReactDOM.render(
<Auth0Provider {...providerConfig}>
<App />
</Auth0Provider>,
document.getElementById("root")
);
});
eurekaHelper.registerWithEureka('auth0-react-sample', PORT);
serviceWorker.unregister();
尤里卡助手。js
const Eureka = require('eureka-js-client').Eureka;
const eurekaHost = (process.env.EUREKA_CLIENT_SERVICEURL_DEFAULTZONE || '127.0.0.1');
const eurekaPort = 8761;
const hostName = (process.env.HOSTNAME || 'localhost')
const ipAddr = '127.0.0.1';
exports.registerWithEureka = function(appName, PORT) {
const client = new Eureka({
instance: {
app: appName,
hostName: hostName,
ipAddr: ipAddr,
port: {
'$': PORT,
'@enabled': 'true',
},
vipAddress: appName,
dataCenterInfo: {
'@class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',
name: 'MyOwn',
},
},
//retry 10 time for 3 minute 20 seconds.
eureka: {
host: eurekaHost,
port: eurekaPort,
servicePath: '/eureka/apps/',
maxRetries: 10,
requestRetryDelay: 2000,
},
})
client.logger.level('debug')
client.start( error => {
console.log(error || "user service registered")
});
function exitHandler(options, exitCode) {
if (options.cleanup) {
}
if (exitCode || exitCode === 0) console.log(exitCode);
if (options.exit) {
client.stop();
}
}
client.on('deregistered', () => {
process.exit();
console.log('after deregistered');
})
client.on('started', () => {
console.log("eureka host " + eurekaHost);
})
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
};
共 (0) 个答案