【问题标题】:Node.js POST Request through Angular application returns Error 404 “not found"通过 Angular 应用程序的 Node.js POST 请求返回错误 404“未找到”
【发布时间】:2020-07-13 06:13:01
【问题描述】:

我正在使用连接到 SQL Server 数据库的 Node.js 创建一个 API。我的 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);
};

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

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,找不到错误 404。

错误:“错误:无法发布 /api/users/new” 消息:“http://url:port/api/users/new 的 Http 失败响应:404 Not Found” 名称:“HttpErrorResponse” 好的:假的 状态:404 状态文本:“未找到” 网址:“http://url:port/api/users/new”

服务文件中的角码如下:

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);
  });
}

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

【问题讨论】:

    标签: node.js angular api post


    【解决方案1】:

    您收到 404(未找到)错误,因为您的 POST 路由定义为 /user/new,但在您的 Angular 应用程序中,您调用的是 http://url:port/api/用户/新

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

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

    【讨论】:

      猜你喜欢
      • 2021-01-17
      • 2021-04-06
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2019-12-12
      • 2015-07-19
      • 1970-01-01
      • 2020-10-21
      相关资源
      最近更新 更多