简体   繁体   English

即使在节点服务器中使用npm cors插件后,CORS也会出现问题

[英]CORS issues even after using npm cors plugin in node server

I have created a simple server in node js to take the request from a react app. 我在节点js中创建了一个简单的服务器,以接收来自React应用程序的请求。

But for the GET method there is no CORS error but whenever I do post, it gives me an error. 但是对于GET方法,没有CORS错误,但是每当我发布时,都会给我一个错误。

在此处输入图片说明

For the POST method to work, I have implemented in index.js file of the actions folder and it should hit the url from the server.js file. 为了使POST方法起作用,我已经在actions文件夹的index.js文件中实现了它,并且应该从server.js文件中找到URL。

index.js index.js

import axios from 'axios';

export const GET_NAVBAR = "GET_NAVBAR";
export const LOGIN = "LOGIN";

export const BASE_API_URL = "http://localhost:3030";
export const GUEST_API_URL = "https://XXX.XXX.XXX.X:5443/wcs/resources/store/1";


export const getNavbar = () => {
    return axios.get(BASE_API_URL + '/topCategory').then(res => {
        return {
            type: GET_NAVBAR,
            payload: res.data.express.catalogGroupView
        };
    });
};



export const login = () => {
    return axios.post(GUEST_API_URL + '/guestidentity', {}).then(res => {
        console.log(res);
        return {
            type: LOGIN,
            payload: {}
        }
    }).catch(e => {
        console.log(e);
        return {
            type: LOGIN,
            payload: {}
        }
    });
};

server.js server.js

const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const Client = require('node-rest-client').Client;//import it here
const app = express();
const helmet = require('helmet');
const morgan = require('morgan');


// enhance your app security with Helmet
app.use(helmet());

// use bodyParser to parse application/json content-type
app.use(bodyParser.json());

app.use(cors());

// log HTTP requests
app.use(morgan('combined'));



app.post('/guestidentity', (req, res) => {

    var client = new Client();

// direct way
    client.post("https://XXX.XXX.XXX.X:5443/wcs/resources/store/1/guestidentity", (data, response) => {
        res.send({express: data});
    });
});



const port = 3030;
app.listen(port, () => console.log(`Server running on port ${port}`));

I don't know where my code is getting wrong. 我不知道我的代码在哪里出错。 Can anybody please help me to troubleshoot this issue. 任何人都可以帮助我解决此问题。 I would be grateful if someone could provide an insight or guide me a little. 如果有人可以提供一些见解或指导我,我将不胜感激。 Thanks 谢谢

For my part I used 就我而言,我曾经

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();
});

It will accept from any * sources, you might want to change that later 它会从任何*来源接受,您以后可能需要更改它

In your server.js , add the following middleware. 在server.js中,添加以下中间件。

var allowCrossDomain = function(req, res, next) {

res.header('Access-Control-Allow-Origin', 'http://localhost:3030/');

res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');

res.header('Access-Control-Allow-Headers', 'Content-Type');


next();

};

app.use(allowCrossDomain);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM