How to send a custom http status message in node / express?

前端 未结 9 749
我寻月下人不归
我寻月下人不归 2020-12-12 16:24

My node.js app is modeled like the express/examples/mvc app.

In a controller action I want to spit out a HTTP 400 status with a custom http message. By default the

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 16:42

    At server side(Express middleware):

    if(err) return res.status(500).end('User already exists.');
    

    Handle at Client side

    Angular:-

    $http().....
    .error(function(data, status) {
      console.error('Repos error', status, data);//"Repos error" 500 "User already exists."
    });
    

    jQuery:-

    $.ajax({
        type: "post",
        url: url,
        success: function (data, text) {
        },
        error: function (request, status, error) {
            alert(request.responseText);
        }
    });
    

提交回复
热议问题