0

I have included the cors package tries the res.header and still getting the cors error

Access to XMLHttpRequest at 'localhost:3000/auth' from origin 'http://localhost:4200' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.

I am running my angular project at localhost:4200 and the node server at 3000 here is the node code:


app.js

    require("./src/db/mySQLConnet");
    const express = require("express");
    const AllFunction = require("./src/routes/allFuntions");
    const cors = require("cors");
    const app = express();
    app.use((req, res, next) => {
      res.setHeader("Access-Control-Allow-Origin", "*"); 
      res.setHeader("Access-Control-Allow-Headers", "*");
      res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
      console.log(req);
      next();
    });
    const port = 3000;
    app.use(cors());
    app.use(express.json());
    app.use(AllFunction);
    app.listen(port, () => {
      console.log("Server started on port 3000");
    });

the allFunctions.js file

    const express = require("express");
    const router = new express.Router();
    const db = require("../db/mySQLConnet");
    //Check User
    router.post("/auth", (req, res) => {
      let Username = req.body.username;
      let Password = req.body.password;
      let sql = "SELECT * FROM user WHERE username=? AND password=?";
      console.log("here");
      let query = db.query(sql, [Username, Password], (err, result) => {
        if (err) throw err;
        res.send(result);
      });
    });
    module.exports = router;

the mysqlConnect.js

    const mysql = require("mysql");
    const db = mysql.createConnection({
      host: "localhost",
      user: "root",
      password: "123456",
      database: "company", 
    });
    //Connect
    db.connect((err) => {
      if (err) {
        throw err;
      }
    console.log("Mysql Connected....");
    });
    module.exports = db;

My angular request is simple

    checkUser(body: any) {
        return this.http.post(`${this.API_URL}/auth`, body);
      }
    this.requestService.checkUser(this.loginFrom?.value).subscribe((res) => {
          console.log(res);
      });
2
  • 2
    You should not set your own CORS headers and also use the CORS middleware. The point of the middleware is to do this for you. I also think there may be something wrong with your API_URL. What's in it? Commented Apr 24, 2024 at 1:59
  • Evert is right: get rid of that app.use((req, res, next) => ... statement and rely solely on Express.js's CORS middleware. And the error message tells you pretty much exactly what's wrong: this.API_URL should be http://localhost:3000/auth (not the presence of scheme), not localhost:3000/auth. Commented Apr 24, 2024 at 7:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.