【问题标题】:404 error on post request in ec2 serverec2 服务器中的 post 请求出现 404 错误
【发布时间】:2015-10-16 16:35:02
【问题描述】:

我将我的平均堆栈项目部署到亚马逊 ec2 实例,我尝试登录,但它返回 404 错误(无法发布 /api/user/login)。客户端和服务器端代码都在同一个项目中。当我从 localhost 运行项目时,它工作正常。可能是什么问题 ?谢谢。

编辑:添加了代码片段

来自控制器

var credentials = {
            email : $scope.user.email,
            password : $scope.user.password
        };


        User.login(credentials)
            .then(function (response) {

                // Administrator. Redirect to /admin.html page
                if($cookies.get('userType') == 0) { 
                    $location.path('/admin');
                }

                console.log("From controller "+ response);

            })
            .catch(function(error) {
                console.log(error);
            })

来自服务

            $http({
                method : 'POST',
                url : '/api/user/login',
                data : data
            })
            .success(function (data, status, headers, config) {
                object = JSON.parse(Util.toObject(data));console.log(object._id);
                $cookies.put('userType', object['status']); // 0 for admin
                $cookies.put('email', object.email); // store the email for session.
                console.log(object['status']  + ' ' +  object.email);
                $('#loginModal').hide();
                deffered.resolve(Util.toObject(data));
            })
            .error(function (data, status, headers, config) {
                console.log("Error in Login "+ status);
                deffered.reject(data);
            })

BE路线

app.use('/api/admin', require('./admin'));

在 admin 文件夹内,index.js 文件

router.post('/login', controller.login);

controller.login 文件

exports.login = function(req, res) {
console.log(req.body);
User.find(req.body, function(err, data) {
    if (err) {
        console.log(' Error in finding the data .. ');
    }
    else {
        console.log(' Data found in login ' + JSON.stringify(data));
        res.status(200).json(data);
    }
});
};

【问题讨论】:

  • 你将不得不提供更多关于这个的细节。由于您获得了404,听起来您的服务器正在运行,但它没有找到您的端点。您确定您的应用程序被正确托管和服务吗?
  • @birryree 这是我遇到的错误。 POST ec2-52-88-27-247.us-west-2.compute.amazonaws.com:9000/api/user/… 404(未找到)(匿名函数)登录错误 404 controllers.js:30 无法 POST /api/user/login
  • @birryree 我从 bitbucket 克隆了项目并安装了正确的软件包。我错过了任何配置吗?
  • 这是什么项目?如果没有关于您正在运行的内容以及控制器的代码外观的任何详细信息,我真的无能为力。
  • @birryree 添加了代码 sn-p。请检查

标签: api amazon-ec2 http-post http-status-code-404


【解决方案1】:

最后找到了问题。我在每个 API 端点的末尾添加了“/”,它起作用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多