POST http:// localhost:3000 / login / aa / aa 404(未找到)

问题描述 投票:0回答:2

我有一个角度应用和一个nodejs后端服务器。我想从后端获取数据,但是当我尝试使用Angular HTTPClient连接到它时,它说:POSThttp://localhost:3000/login/aa/aa404(未找到)。但是,当我手动将链接放到浏览器中时,它工作得很好。这是一些代码:

service.ts

addUser(user: IUser): Observable<IUser> {
        return this.httpClient.post<IUser>(`http://localhost:3000/login/${user.email}/${user.passwort}`, user, {
            headers: new HttpHeaders({
                'Content-Type': 'application/json'
            })
        })
            .pipe(catchError(this.handleError));
    }

index.js

var mysql = require('mysql');
var express = require('express');
var app = express();

const port = process.env.PORT || 3000;
[...]
app.get('/login/:email/:pw',function(req,res) {
  res.setHeader('Content-Type', 'application/json');
  var passwort = new Passwort(''+req.params.pw);
  passwort.comparePasswort();
  con.query("SELECT u.Email, u.Hash FROM User u WHERE u.Email LIKE "+ "'" + req.params.email+ "'", function(err, result ){
    if(err) throw err;
    console.log(result)
    res.send("test")
  })

});

感谢您的每一个回答和您的时间!

node.js angular http-status-code-404 backend
2个回答
1
投票
您在后端的路由设置为get请求,而不是post请求。

您应该使用get将您的请求转换为服务中的this.httpClient.get...,或者使用post将其转换为后端的app.post请求。

它在您的浏览器中起作用的原因是,使用地址栏访问某些内容时,浏览器会执行GET请求。


0
投票
在后台,您声明了一个get方法,而从前端开始,您正在调用post。您正在使用的代码应为:-

addUser(user: IUser): Observable<IUser> { return this.httpClient.get<IUser>(`http://localhost:3000/login/${user.email}/${user.passwort}`, { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }) .pipe(catchError(this.handleError)); }


0
投票
在使用/:email之前,您需要预订此

particular元素

const mongoose = require("mongoose"); const User = mongoose.model("User"); const userParams = (req, res, next, email) => { User.findOne({email:email}) .then((user)=> { if (!user) { return res.sendStatus(404); } req.user = user; return next(); }) .catch(next); }; module.exports = userParams;
然后通过键入]在快速路由器中使用它>

router.param("email", userParams);

通过这种方式,您的路由器将了解您要发送的参数

0
投票
在index.js文件中,您正在为GET请求创建处理程序(这是浏览器在访问网页时发送的默认请求)
© www.soinside.com 2019 - 2024. All rights reserved.