如何使用Express通过角度服务器发送数据? 404错误

时间:2019-07-04 02:59:32

标签: angular express

我正在尝试使用angular从前端获取用户注册详细信息,并将其通过服务器发送(快速)。我不确定自己在做什么错。有什么办法吗? 我将注册页面的路径命名为“ registration”,这就是为什么我在post api中使用“ / registration”的原因。

user-register.component.ts


  registerUser(event) {
    event.preventDefault();
    const target = event.target;
    const registerName = target.registerName.value;
    const registerEmail = target.registerEmail.value;
    const registerPassword = target.registerPassword.value;
    const companyName = target.companyName.value;
    const companyCategory = target.companyCategory.value;


    this.auth.registerUser(registerName, registerEmail, registerPassword, companyName, companyCategory).subscribe(data => {
      console.log(data);
      /*if (data.success) {
        this.router.navigate(['login']);
      }*/
    });
    console.log(registerName)
  }

auth.service.ts:


  registerUser(registerName, registerEmail, registerPassword, companyName, companyCategory) {
    return this.http.post('/registration', {
      registerName,
      registerEmail,
      registerPassword,
      companyName,
      companyCategory
    });
  }


server folder : index.js

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.json());

app.post('/registration', (req, res) => {

console.log(req.body);
});

app.listen (4200, ()=> console.log('Server listen at 4200'));

1 个答案:

答案 0 :(得分:0)

我发现问题出在auth.service.ts上: 我改变了:

registerUser(registerName, registerEmail, registerPassword, companyName, companyCategory) {
    return this.http.post('/registration', {
      registerName,
      registerEmail,
      registerPassword,
      companyName,
      companyCategory
    });
  }

对此

registerUser(registerName, registerEmail, registerPassword, companyName, companyCategory) {
    return this.http.post('http://localhost:SERVERPORT/registration', {
      registerName,
      registerEmail,
      registerPassword,
      companyName,
      companyCategory
    });
  }