req.params.username 返回为:用户名

req.params.username returning as :username

我的网络应用程序中有一条路线,我试图通过路线参数传递用户名。这是我的 script.js 附加到我的 index.html:

$scope.del_movie = function(movie) {
        $http( {
            method: 'DELETE',
            url: '/movie/:title/:username',
            params: {'title': movie.title, 'username': movie.username}
        }).then(function successCallback(response) {
            console.log(response);
            return getData();
        }, function errorCallback(response) {
            console.log('Error: ' + response);
        });
    };

我在 server.js 中设置的路线如下所示:

app.delete('/movie/:title/:username', requiresAuth(), function(req, res) {
    paramsUsernameString = req.params.username;
    oidcEmailString = JSON.stringify(req.oidc.user.email);


    console.log("movie " + req.params.username);

    if(paramsUsernameString != oidcEmailString){
        console.log("req.params.username " + paramsUsernameString + " req.oidc.user.username " + oidcEmailString);
        console.log("can't delete someone else's review!");
        res.json({
            message: "You can't delete someone else's review, as much as you may like to"
        });
    }
    else{
        Movie.findOneAndRemove(req.query, function(err, result) {
            if ( err ) throw err;
            res.json( {
                message: "req.params.username " + paramsUsernameString + " req.oidc.user.username " + oidcEmailString,
                movie: result
            });
        });
    }
});

问题是,当我 console.log req.params.username 时,它并没有作为我使用 del_movie 传入的用户名出现。它显示为“:用户名”。我查找了快速路由,但我对自己做错了什么感到困惑;我的路线看起来像所有的例子,除非我遗漏了一些明显的东西。

我需要 expressbodyParserexpress-openid-connect,以及我在 model.js 中的电影架构,它具有用户名和标题。

感谢任何答案,如果需要,很高兴提供更多 info/context!

根据https://docs.angularjs.org/api/ng/service/$http

params – {Object.<string|Object>} – Map of strings or objects which will be serialized with the paramSerializer and appended as GET parameters.

这意味着您的 url 现在变成:

 `/movie/:title/:username?title=${movie.title}&username=${movie.username}`

因此,在您的服务器上,req.params.username 将是硬编码的 ":username"

是正确的

如果你不想修改路线,那么应该req.query.username你想要的。