我有一个CRUD应用程序,您可以在其中创建发行版。
用户可以添加发布(发布具有版本和说明)
这是通过一个按钮以及一些可以在其中输入详细信息的字段完成的

    <button (click)="addRelease(version.value, description.value)" [disabled]="angForm.pristine || angForm.invalid" class="btn btn-primary">Add</button>


我的addRelease函数:

    addRelease(version, description) {
this.releaseservice.addRelease(version, description);
}


我的服务中的addRelease函数:

    addRelease(version,description) {
const uri = 'http://localhost:4000/releases/add';
const obj = {
  version: version,
  description: description
};
this
  .http
  .post(uri, obj)
  .subscribe(res =>
      console.log('Done'));
}


然后在我的releaseRoutes.js中

   releaseRoutes.route('/releases/add').post(function (req, res) {
   var release = new Release(req.body);
   release.save()
   .then(item => {
   res.status(200).json({'release': 'release added successfully'});
   })
   .catch(err => {
   res.status(400).send("unable to save to database");
    });
   });


server.js:

   releaseRoutes = require('./expressRoutes/releaseRoutes')
   const app = express();
   app.use('/releases', releaseRoutes);


省略了一些代码以使其更易于阅读。

当我尝试创建新版本时
它给出了404错误

    POST http://localhost:4000/releases/add 404 (Not Found)
   core.js:1449 ERROR HttpErrorResponse {headers: HttpHeaders, status: 404,statusText: "Not Found", url: "http://localhost:4000/releases/add", ok: false, …}


我对此并不陌生,需要一些帮助来解决此错误,并坚持了几天

最佳答案

您已经在/releases中定义了app.use('/releases', releaseRoutes);的路由

releaseRoutes.route('/releases/add')更改为releaseRoutes.route('/add')

10-06 02:30