Giving cors error i have already implemented cors

Viewed 23

I have already implemented cors but the code is giving me cors error. Please tell me where I am lacking. I am making post request and specifying all methods and body

import express from 'express';
import { APP_PORT, DB_URL } from './config'
import errorHandler from './middlewares/errorHandler';
import routes from './routes'
import path from 'path';
import cors from 'cors';

import mongoose from 'mongoose';

const app = express();
app.use(express.urlencoded({extended:true}));
app.use(express.json());

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.use('/api', routes);

mongoose.connect(DB_URL,{useNewUrlParser:true, useUnifiedTopology:true});
const db = mongoose.connection;
db.on('error', console.error.bind(console,'connection-error'));
db.once('open',()=>{
    console.log('DB connected');
})

global.appRoot = path.resolve(__dirname);

app.use('/uploads',express.static('uploads'))




app.use(errorHandler)
app.listen(APP_PORT,()=>{
    console.log(`server is running on port number ${APP_PORT}`)
})

This is the fetch call I am making to get data

fetch('http://localhost:5000/api/products/cart-items',{
    method:'POST',
    headers:{'Content-Type':'application/json'},
    body:{
       'ids':["632885878b19757d761dfbf8","6328850a8b19757d761dfbf2"] 
    }
})
0 Answers
Related