the problem I have server-side build in node.js and client build in angular and create API POST and GET, put show me the Cors problem
this is client side angular
import { Component, OnInit } from '@angular/core';
import { Http, Response } from "@angular/http";
import { Router } from '@angular/router';
import { HttpClient , HttpClientModule , HttpHeaders } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.login.html',
styleUrls: ['./app.login.css'],
})
export class AppComponent {
title = 'app';
constructor(private http: Http){
this.LogIn();
this.ping();
}
LogIn() {
return this.http.post("localhost:3000/api/login/checklogin", {
username: 'mohsen',
password: '2345'
})
.subscribe(
res => {
console.log(res.json());
},
err => {
console.log("Error occured")
}
);
}
const express = require('express');
const router = express.Router();
var app = express();
const listUsers = [
{"username" : "wael", "password" : "1234"},
{"username" : "mohsen", "password" : "2345"}
];
router.get('/ping.json' ,(req, res, next) => {
res.status(200).json({
message: 'pong'
});
});
// router.post('/checklogin',(req, res ,next) =>{
router.post('/checklogin',(req, res ,next) =>{
console.log(req.body);
for(let i =0 ; i <listUsers.length ; i ++){
console.log(listUsers[i]);
if(listUsers[i].username === req.body.username && listUsers[i].password === req.body.password){
res.status(200).json({
message: "Success"
});
return;
}
}
// not found
res.status(200).json({
message: "Failed"
});
});
the problem shows me this message No 'Access-Control-Allow-Origin' header is present on the requested resource” error.