Node.js通过Angular应用程序的POST请求返回错误404“;“未找到”;

Node.js通过Angular应用程序的POST请求返回错误404“;“未找到”;,node.js,angular,api,post,Node.js,Angular,Api,Post,我正在使用Node.js制作一个API,该API连接到SQL Server数据库。我的GET请求工作正常,但我的POST请求给了我错误。我已将节点项目分为两个文件,一个路由文件和一个控制器文件 “我的路线”文件中的代码如下所示: module.exports = (app) => { const UsrContrllr = require('../Controllers/users.controllers'); //1. GET ALL USERS app.get('/a

我正在使用Node.js制作一个API,该API连接到SQL Server数据库。我的GET请求工作正常,但我的POST请求给了我错误。我已将节点项目分为两个文件,一个路由文件和一个控制器文件

“我的路线”文件中的代码如下所示:

module.exports = (app) => {
  const UsrContrllr = require('../Controllers/users.controllers');
  
  //1. GET ALL USERS
  app.get('/api/users', UsrContrllr.getAllUsers);

        
  //2. POST NEW USER
  app.post('/api/user/new', UsrContrllr.addNewUser);
};

private url = 'http://url:port/api/users';
signup(fName: string, lName: string, usrCode: string, pwd: string, cntctNbr: string) {
  const headers = new HttpHeaders().set('Content-Type', 'application/json');  
  const newUsr = {
    usercode: usrCode,
    password: pwd,
    firstname: fName,
    lastname: lName,
    contactno: cntctNbr
  }
  
  this.http.post(this.url + '/new', JSON.stringify(newUsr), { headers: headers }).subscribe(data => {
      console.log(data);
  });
}

我的控制器文件中的代码如下所示:

const mssql = require('mssql');

exports.getAllUsers = (req, res) => 
{
    // Validate request
    console.log(`Fetching RESPONSE`);
    // create Request object
    var request = new mssql.Request();
    // query to the database and get the records
    const queryStr = `SELECT * FROM USERS`;
    request.query(queryStr, function (err, recordset) {
        if (err) console.log(err)
        else {
            if (recordset.recordset.toString() === '') {
                res.send('Oops!!! Required data not found...');
            }
            else {
                // send records as a response
                res.send(recordset);
            }
        };
    });
};

exports.addNewUser = (req, res) =>
{
     // Validate request
     console.log(`INSERTING RECORD ${req.body}`);
     // create Request object
     var request = new mssql.Request();
     // query to the database and get the records
     const queryStr = `INSERT INTO USERS (USERCODE, PASSWORD, LANGUAGE, USERCLASS, FIRSTNAME, LASTNAME, CONTACTNO) VALUES ('${req.body.usercode}', '${req.body.password}', 'EN', '0', '${req.body.firstname}', '${req.body.lastname}', '${req.body.contactno}');`;
     console.log(queryStr);
     request.query(queryStr, function (err, recordset) {
         if (err) console.log(err)
         else {
             if (recordset.recordset.toString() == '') {
                 res.send('Oops!!! Required data not found...');
             }
             else {
                 // Send records as response
                 res.send(recordset);
             }
         };
    });
};
当我从angular应用程序运行POST请求时,我得到一个HttpErrorResponse,错误404NotFound

错误:“错误:无法发布/api/users/new” 消息:“的Http失败响应”http://url:port/api/users/new: 404找不到“ 名称:“HttpErrorResponse” 好:错 现状:404 statusText:“未找到” url:“http://url:port/api/users/new"

服务文件中的角度代码如下所示:

module.exports = (app) => {
  const UsrContrllr = require('../Controllers/users.controllers');
  
  //1. GET ALL USERS
  app.get('/api/users', UsrContrllr.getAllUsers);

        
  //2. POST NEW USER
  app.post('/api/user/new', UsrContrllr.addNewUser);
};

private url = 'http://url:port/api/users';
signup(fName: string, lName: string, usrCode: string, pwd: string, cntctNbr: string) {
  const headers = new HttpHeaders().set('Content-Type', 'application/json');  
  const newUsr = {
    usercode: usrCode,
    password: pwd,
    firstname: fName,
    lastname: lName,
    contactno: cntctNbr
  }
  
  this.http.post(this.url + '/new', JSON.stringify(newUsr), { headers: headers }).subscribe(data => {
      console.log(data);
  });
}

我不明白为什么我无法添加新用户。有人告诉我,我的代码看起来不错,但我看不到任何结果。我哪里出错了?

您收到的是404(未找到)错误,因为您的投递路线定义为/用户/new,但在您的angular应用程序中您正在呼叫http://url:port/api/用户/new

在API中将代码更正为以下内容:

app.post('/api/users/new', UsrContrllr.addNewUser);