ghimaji zied
ghimaji zied

Reputation: 23

Access-Control-Allow-Origin’ missing - reactjs

I'm building a signup page with react, I am trying to make an HTTP request with axios.get when my component mounts but I'm getting the No 'Access-Control-Allow-Origin:

enter image description here

Here is my server.js code:

// server.js
    const express = require('express');
    const mongoose = require('mongoose');
    const bodyParser = require("body-parser");
    const users = require("./routes/users");
    const profile= require("./routes/profilew");
    const post= require("./routes/post");
    
    const app = express();
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(bodyParser.json());
    // config db
    const db = require('./config/Keys').mongoURI ;
    mongoose
      .connect(db)
      .then(() => console.log("MongoDB Connected"))
      .catch(err => console.log(err));
    
    app.use("/users", users);
    app.use("/profile", profile);
    app.use("/post", post);
    
    
    app.get('/',(req,res)=>{
        res.send('hello')
    })
    
    
    
    app.listen(5000, err => {
        if (err) console.log("connection to server failed");
        console.log("connected on port 5000");
      });

Upvotes: 1

Views: 52

Answers (1)

PasVV
PasVV

Reputation: 593

Your server should allow requests from :3000 port. Usually, cross-domain requests disable on most servers (I mean IIS/Apache/Node).

You should set access-control-allow-origin on your server with http://localhost:3000 or * value. How to do it see here.

Read more about cross-domain restrictions in specs.

Upvotes: 1

Related Questions