如何解决问题:zone.js:3243 POST http:// localhost:4200 / login 404(未找到)

时间:2019-07-16 16:04:05

标签: mysql angular express

我想为我的页面做一个简单的登录功能。我为此页面使用Angular-MySQL-Express-Sequelize。 当我执行POST登录时,它总是告诉我 zone.js:3243 POST http://localhost:4200/login 404(未找到) 我不知道这意味着什么以及如何解决。

这是我的customer.service.ts

export interface Details {
  username: string;
  password: string;
  age: number;
  type: string;
}
interface TokenResponse {
  token: string;
}
export interface TokenPayLoad {
  username: string;
  password: string;
}
public login(customer: TokenPayLoad): Observable<any> {
    const base = this.http.post('/login', customer);

    const request = base.pipe(
      map((data: TokenResponse) => {
        if (data.token) {
          this.saveToken(data.token);
        }
        return data;
      })
    );

    return request;
  }

////

这是我的login.component.ts

credentials: TokenPayLoad = {
    username: '',
    password: '',
  };

  constructor(private customerService: CustomerService, private router: Router){}
  ngOnInit() {
  }

  login() {
    this.customerService.login(this.credentials).subscribe (
      () => {
        this.router.navigateByUrl('/customer');
      },
      err => {
      }
    );
  }

///// 这是我的后端登录功能

exports.login = (req, res) =>{
    Customer.findOne(
        {where: {
            username: req.body.username,
            password: req.body.password
            }
        }).then(customer => {
        if(req.body.password = customer.password){
            let token = jwt.sign(customer.dataValues, secret_key, {
                expiresIn: 1400
            })
                    res.json({token: token});
        }
        else{
            res.send('user not exists');
        }
    })
        .catch(err => {
            res.send('error is ' + err);

    })
}

1 个答案:

答案 0 :(得分:0)

假设您正在使用Angular-CLI创建项目。您将需要代理您的API(如果您想使用相同的端口,即4200)。

以下文章向您展示了如何使用Angular CLI配置代理

https://itnext.io/angular-cli-proxy-configuration-4311acec9d6f