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