Я пытаюсь разработать проект MERN.Мой код в server.js
файле express.js
выглядит следующим образом.
const express = require('express');
const bodyParser = require('body-parser');
const addressroute = require('./api/routes/address');
const userroute = require('./api/routes/user');
const mongoose = require('mongoose');
mongoose.set('useCreateIndex', true);
mongoose.connect('mongodb://localhost/addresses', { useNewUrlParser: true });
const db = mongoose.connection;
var cors = require('cors');
let app = express();
app.use(bodyParser.urlencoded( { extended:true } ));
app.use(bodyParser.json());
app.use('/api/address', addressroute);
app.use('/api/users', userroute);
app.use(cors());
app.options('*', cors());
const PORT = process.env.PORT || 4000;
app.listen(PORT);
Я установил CORS, используя npm install cors --save
эту команду.
Я получаю ошибку ниже.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:4000/api/users/register. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).[Learn More]
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:4000/api/users/register. (Reason: CORS request did not succeed).