为每个Hapi路线添加状态代码

时间:2017-10-30 16:16:44

标签: javascript node.js hapijs

有没有办法在Hapi中为每条路线定义要返回的状态代码?

例如,

routes.js

server.route(routes);

server.js

responseCode

在用户控制器中,我希望能够阅读class Resource { constructor() {} async request(handler, request, reply) { try { const response = await handler; const statusCode = request.custom.responseCode; // how? reply(response).code(statusCode); } catch(error) { reply(error); } } } class User extends Resource { constructor(props) { super(props); } list(request, reply) { const getUsers = UserController.list(); super.request(getUsers, request, reply); } } 之类的内容:

 <script>
$(function () {    

    // Create the chart
    $('#leftnav').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: 'Basic drilldown'
        },
        xAxis: {
            type: 'category'
        },

        legend: {
            enabled: true
        },

        plotOptions: {
            series: {
                borderWidth: 0,
                dataLabels: {
                    enabled: true,
                    style: {
                        color: 'white',
                        textShadow: '0 0 2px black, 0 0 2px black'
                    }
                },
                stacking: 'normal'
            }
        },

        series: [{
            name: 'Things',
            data: [{

                name: 'Animals',
                y: 5,
                drilldown: 'animals'
            }, {
                name: 'Fruits',
                y: 2,
                drilldown: 'fruits'
            }, {
                name: 'Cars',
                y: 4,
                drilldown: 'cars'
            }]
        }, {
            name: 'Things2',
            data: [{
                name: 'Animals',
                y: 1,
                drilldown: 'animals2'
            }, {
                name: 'Fruits',
                y: 5,
                drilldown: 'fruits2'
            }, {
                name: 'Cars',
                y: 2,
                drilldown: 'cars2'
            }]
        }, {
            name: 'Things3',
            stack: 1,
            data: [{
                name: 'Animals',
                y: 8,
                drilldown: 'animals3'
            }, {
                name: 'Fruits',
                y: 7,
                drilldown: 'fruits3'
            }, {
                name: 'Cars',
                y: 10,
                drilldown: 'cars3'
            }]
        }],
        drilldown: {
            activeDataLabelStyle: {
                color: 'white',
                textShadow: '0 0 2px black, 0 0 2px black'
            },
            series: [{
                id: 'animals',
                name: 'Animals',
                data: [
                    ['Cats', 4],
                    ['Dogs', 2],
                    ['Cows', 1],
                    ['Sheep', 2],
                    ['Pigs', 1]
                ]
            }, {
                id: 'fruits',
                name: 'Fruits',
                data: [
                    ['Apples', 4],
                    ['Oranges', 2]
                ]
            }, {
                id: 'cars',
                name: 'Cars',
                data: [
                    ['Toyota', 4],
                    ['Opel', 2],
                    ['Volkswagen', 2]
                ]
            },{
                id: 'animals2',
                name: 'Animals2',
                data: [
                    ['Cats', 3],
                    ['Dogs', 5],
                    ['Cows', 6],
                    ['Sheep', 2],
                    ['Pigs', 2]
                ]
            }, {
                id: 'fruits2',
                name: 'Fruits2',
                data: [
                    ['Apples', 1],
                    ['Oranges', 5]
                ]
            }, {
                id: 'cars2',
                name: 'Cars2',
                data: [
                    ['Toyota', 2],
                    ['Opel', 3],
                    ['Volkswagen', 2]
                ]
            },{
                id: 'animals3',
                name: 'Animals3',
                stack: 1,
                data: [
                    ['Cats', 2],
                    ['Dogs', 3],
                    ['Cows', 1],
                    ['Sheep', 1],
                    ['Pigs', 1]
                ]
            }, {
                id: 'fruits3',
                name: 'Fruits3',
                stack: 1,
                data: [
                    ['Apples', 4],
                    ['Oranges', 3]
                ]
            }, {
                id: 'cars3',
                name: 'Cars3',
                stack: 1,
                data: [
                    ['Toyota', 4],
                    ['Opel', 3],
                    ['Volkswagen', 3]
                ]
            }]
        }
    })
});

    </script>

1 个答案:

答案 0 :(得分:1)

我认为你的处理程序应该返回带有所需状态代码的响应。通过这种方式,您将能够根据业务逻辑设置代码(例如,如果新用户已存在于数据库中,则为代码409)。您可以通过简单地将响应包装在{data,statusCode}对象中来实现。

示例:

item = soup.find("tr").find_next_sibling().find('td').text

之后,您将能够访问该statusCode:

const response = { data: users, statusCode: 200 } 
return response;

看起来太简单了,但实际上并不需要过度设计。