lib/router.js
/**
 * @brief This function sets up the express router to handle POST requests.
 *
 * @return object express router.
 */
@brief This function sets up the express router to handle POST requests.
@return object express router.
@brief This function sets up the express router to handle POST requests . @return object express router .
function initializeRouter() {
    'use strict';
    const express = require('express');
    const router = express.Router();
    const constants = require('../lib/constants');
    const funcMap = require('../commands');
    const logger = require('./logger.js').create();
    function postHandler(req, res) {
        let text = req.body.text;
        let args = text.trim().split(" ");
        let command;
        if (args.length === 0) {
            command = constants.commands.HELP;
        } else {
            command = args[0];
        }
        let handler = funcMap.get(command);
        if (handler === undefined) {
            command = constants.commands.HELP;
            handler = funcMap.get(command);
            req.body.errorMessage = "You have entered an invalid command!";
        }
        if (handler.parseArgs(req) === false) {
            handler = funcMap.get(constants.commands.HELP);
        }
        logger.trace({requestBody: req.body}, "POST body checking done.");
        handler.handle(req, res);
    }
    router.get('/', function (req, res) {
        logger.info({req}, "GET request received");
        return res.status(200).end("OK");
    });
    router.post('/', postHandler);
    return router;
}
function initializeRouter ( ) { 'use strict' ; const express = require ( 'express' ) ; const router = express . Router ( ) ; const constants = require ( '../lib/constants' ) ; const funcMap = require ( '../commands' ) ; const logger = require ( './logger.js' ) . create ( ) ; function postHandler ( req , res ) { let text = req . body . text ; let args = text . trim ( ) . split ( " " ) ; let command ; if ( args . length === 0 ) { command = constants . commands . HELP ; } else { command = args [ 0 ] ; } let handler = funcMap . get ( command ) ; if ( handler === undefined ) { command = constants . commands . HELP ; handler = funcMap . get ( command ) ; req . body . errorMessage = "You have entered an invalid command!" ; } if ( handler . parseArgs ( req ) === false ) { handler = funcMap . get ( constants . commands . HELP ) ; } logger . trace ( { requestBody : req . body } , "POST body checking done." ) ; handler . handle ( req , res ) ; } router . get ( '/' , function ( req , res ) { logger . info ( { req } , "GET request received" ) ; return res . status ( 200 ) . end ( "OK" ) ; } ) ; router . post ( '/' , postHandler ) ; return router ; }
@brief This function sets up the express router to handle POST requests.
@brief This function sets up the express router to handle POST requests .
// Access application constants. // Setup map containing command handlers. // Access bunyan logger. /**
     * @brief Middeware function to handle express http POST requests. This
     *        function checks the arguments to the /ttt command and calls
     *        the appropriate handler function.
     *
     * @param req express http request object
     * @param res express http response object
     *
     * @return none
     */ // Check /ttt command arguments available in the 'text' field. // Since no arguments given, defualt to 'help' // First argument represnts the ttt command to be handled. // Get the appropriate handler. // Since no arguments given, defualt to 'help' /* The handler first validates the request body to check if it has all
         * the required data and then proceeds to handle the request.
         */ // Since the request is invalid, execute the 'help' routine. // Call the 'handle' function to generate the response. /* GET handler returning an HTTP 200 response to respond to periodic
     * requests from Slack to verify the certificate.
     */ /* Setup router middleware to handle POST requests */