Node router for marketplace routes

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












After cleaning up & some refactoring, one module of my code became like below.
However, I'm feeling like there's still some unnecessary redundancy, and it can be improved further and look neater.



But I can't come up with exactly how to do it.



'use strict';

// === showing the execution context ==============

const express = require('express');
const router = express.Router();

const moment = require("moment");
const momentDurationSetup = require("moment-duration-format");
const DB = require('./DBOperator');
const _ = require('lodash');

const Promise = require("bluebird");
const request = require('superagent-bluebird-promise');

const utils = require('../utils');


// ========== main concern area ===============

function realTime(queryFunction, payload, req, res, next)
queryFunction((err, results) =>
if (err)
console.log(err);
next();
else
res.json(data: _.get(results[0].rows[0], payload, -1));

);


router.get('/online-buyer-count', utils.mcache(15), (req, res, next) =>
realTime(DB.onlinebuyerNumQuery, 'onlinebuyerNum', req, res, next);
);

router.get('/offline-buyer-count', utils.mcache(15), (req, res, next) =>
realTime(DB.offlinebuyerNumQuery, 'offlinebuyerNum', req, res, next);
);

router.get('/online-seller-count', utils.mcache(15), (req, res, next) =>
realTime(DB.onlinesellerNumQuery, 'onlinesellerNum', req, res, next);
);

router.get('/offline-seller-count', utils.mcache(15), (req, res, next) =>
realTime(DB.offlinesellerNumQuery, 'offlinesellerNum', req, res, next);
);


/ ===========================

module.exports = router;






share|improve this question





















  • Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Feel free to edit and give it a different title if there is something more appropriate.
    – Sam Onela
    May 26 at 2:48










  • @samOnela that makes sense. Thanks for the edit.
    – kmonsoor
    May 27 at 14:30

















up vote
1
down vote

favorite












After cleaning up & some refactoring, one module of my code became like below.
However, I'm feeling like there's still some unnecessary redundancy, and it can be improved further and look neater.



But I can't come up with exactly how to do it.



'use strict';

// === showing the execution context ==============

const express = require('express');
const router = express.Router();

const moment = require("moment");
const momentDurationSetup = require("moment-duration-format");
const DB = require('./DBOperator');
const _ = require('lodash');

const Promise = require("bluebird");
const request = require('superagent-bluebird-promise');

const utils = require('../utils');


// ========== main concern area ===============

function realTime(queryFunction, payload, req, res, next)
queryFunction((err, results) =>
if (err)
console.log(err);
next();
else
res.json(data: _.get(results[0].rows[0], payload, -1));

);


router.get('/online-buyer-count', utils.mcache(15), (req, res, next) =>
realTime(DB.onlinebuyerNumQuery, 'onlinebuyerNum', req, res, next);
);

router.get('/offline-buyer-count', utils.mcache(15), (req, res, next) =>
realTime(DB.offlinebuyerNumQuery, 'offlinebuyerNum', req, res, next);
);

router.get('/online-seller-count', utils.mcache(15), (req, res, next) =>
realTime(DB.onlinesellerNumQuery, 'onlinesellerNum', req, res, next);
);

router.get('/offline-seller-count', utils.mcache(15), (req, res, next) =>
realTime(DB.offlinesellerNumQuery, 'offlinesellerNum', req, res, next);
);


/ ===========================

module.exports = router;






share|improve this question





















  • Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Feel free to edit and give it a different title if there is something more appropriate.
    – Sam Onela
    May 26 at 2:48










  • @samOnela that makes sense. Thanks for the edit.
    – kmonsoor
    May 27 at 14:30













up vote
1
down vote

favorite









up vote
1
down vote

favorite











After cleaning up & some refactoring, one module of my code became like below.
However, I'm feeling like there's still some unnecessary redundancy, and it can be improved further and look neater.



But I can't come up with exactly how to do it.



'use strict';

// === showing the execution context ==============

const express = require('express');
const router = express.Router();

const moment = require("moment");
const momentDurationSetup = require("moment-duration-format");
const DB = require('./DBOperator');
const _ = require('lodash');

const Promise = require("bluebird");
const request = require('superagent-bluebird-promise');

const utils = require('../utils');


// ========== main concern area ===============

function realTime(queryFunction, payload, req, res, next)
queryFunction((err, results) =>
if (err)
console.log(err);
next();
else
res.json(data: _.get(results[0].rows[0], payload, -1));

);


router.get('/online-buyer-count', utils.mcache(15), (req, res, next) =>
realTime(DB.onlinebuyerNumQuery, 'onlinebuyerNum', req, res, next);
);

router.get('/offline-buyer-count', utils.mcache(15), (req, res, next) =>
realTime(DB.offlinebuyerNumQuery, 'offlinebuyerNum', req, res, next);
);

router.get('/online-seller-count', utils.mcache(15), (req, res, next) =>
realTime(DB.onlinesellerNumQuery, 'onlinesellerNum', req, res, next);
);

router.get('/offline-seller-count', utils.mcache(15), (req, res, next) =>
realTime(DB.offlinesellerNumQuery, 'offlinesellerNum', req, res, next);
);


/ ===========================

module.exports = router;






share|improve this question













After cleaning up & some refactoring, one module of my code became like below.
However, I'm feeling like there's still some unnecessary redundancy, and it can be improved further and look neater.



But I can't come up with exactly how to do it.



'use strict';

// === showing the execution context ==============

const express = require('express');
const router = express.Router();

const moment = require("moment");
const momentDurationSetup = require("moment-duration-format");
const DB = require('./DBOperator');
const _ = require('lodash');

const Promise = require("bluebird");
const request = require('superagent-bluebird-promise');

const utils = require('../utils');


// ========== main concern area ===============

function realTime(queryFunction, payload, req, res, next)
queryFunction((err, results) =>
if (err)
console.log(err);
next();
else
res.json(data: _.get(results[0].rows[0], payload, -1));

);


router.get('/online-buyer-count', utils.mcache(15), (req, res, next) =>
realTime(DB.onlinebuyerNumQuery, 'onlinebuyerNum', req, res, next);
);

router.get('/offline-buyer-count', utils.mcache(15), (req, res, next) =>
realTime(DB.offlinebuyerNumQuery, 'offlinebuyerNum', req, res, next);
);

router.get('/online-seller-count', utils.mcache(15), (req, res, next) =>
realTime(DB.onlinesellerNumQuery, 'onlinesellerNum', req, res, next);
);

router.get('/offline-seller-count', utils.mcache(15), (req, res, next) =>
realTime(DB.offlinesellerNumQuery, 'offlinesellerNum', req, res, next);
);


/ ===========================

module.exports = router;








share|improve this question












share|improve this question




share|improve this question








edited May 30 at 18:45









200_success

123k14143399




123k14143399









asked May 26 at 2:30









kmonsoor

1084




1084











  • Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Feel free to edit and give it a different title if there is something more appropriate.
    – Sam Onela
    May 26 at 2:48










  • @samOnela that makes sense. Thanks for the edit.
    – kmonsoor
    May 27 at 14:30

















  • Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Feel free to edit and give it a different title if there is something more appropriate.
    – Sam Onela
    May 26 at 2:48










  • @samOnela that makes sense. Thanks for the edit.
    – kmonsoor
    May 27 at 14:30
















Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Feel free to edit and give it a different title if there is something more appropriate.
– Sam Onela
May 26 at 2:48




Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Feel free to edit and give it a different title if there is something more appropriate.
– Sam Onela
May 26 at 2:48












@samOnela that makes sense. Thanks for the edit.
– kmonsoor
May 27 at 14:30





@samOnela that makes sense. Thanks for the edit.
– kmonsoor
May 27 at 14:30











1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Partial application



One way to simplify the code is to eliminate the lambda/anonymous functions using Function.bind() to create Partially applied functions. For instance:




router.get('/online-buyer-count', utils.mcache(15), (req, res, next) => 
realTime(DB.onlinebuyerNumQuery, 'onlinebuyerNum', req, res, next);
);



can be simplified to



router.get('/online-buyer-count', realTime.bind(null, DB.onlinebuyerNumQuery, 'onlinebuyerNum'));


And similarly for the other three routes, which reduces 12 lines to 4.



looping over the routes



The redundancy could also be simplified using Array.forEach()



['online', 'offline'].forEach(mode => 
['buyer', 'seller'].forEach(role =>
router.get('/'+mode+'-'+role+'-count', realTime.bind(null, DB[mode+role+'NumQuery'], mode+role+'Num'));
);
);


Taking that one step further using more partially-applied functions, the callback function of the inner forEach can be pulled out to a separate function:



const addRoute = (mode, role) => router.get('/'+mode+'-'+role+'-count', realTime.bind(null, DB[mode+role+'NumQuery'], mode+role+'Num'));


And then that function can be made into a partial and used within the nested forEach:



['online', 'offline'].forEach(mode => ['buyer', 'seller'].forEach(addRoute.bind(null, mode)));


So that can reduce those 4 lines to 2. It may be desirable to break those up into multiple lines for readability.






share|improve this answer





















  • Thanks a lot. While the first bind method is more advanced, the second forEach answers my question. It looks more approachable.
    – kmonsoor
    May 30 at 19:34











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195196%2fnode-router-for-marketplace-routes%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Partial application



One way to simplify the code is to eliminate the lambda/anonymous functions using Function.bind() to create Partially applied functions. For instance:




router.get('/online-buyer-count', utils.mcache(15), (req, res, next) => 
realTime(DB.onlinebuyerNumQuery, 'onlinebuyerNum', req, res, next);
);



can be simplified to



router.get('/online-buyer-count', realTime.bind(null, DB.onlinebuyerNumQuery, 'onlinebuyerNum'));


And similarly for the other three routes, which reduces 12 lines to 4.



looping over the routes



The redundancy could also be simplified using Array.forEach()



['online', 'offline'].forEach(mode => 
['buyer', 'seller'].forEach(role =>
router.get('/'+mode+'-'+role+'-count', realTime.bind(null, DB[mode+role+'NumQuery'], mode+role+'Num'));
);
);


Taking that one step further using more partially-applied functions, the callback function of the inner forEach can be pulled out to a separate function:



const addRoute = (mode, role) => router.get('/'+mode+'-'+role+'-count', realTime.bind(null, DB[mode+role+'NumQuery'], mode+role+'Num'));


And then that function can be made into a partial and used within the nested forEach:



['online', 'offline'].forEach(mode => ['buyer', 'seller'].forEach(addRoute.bind(null, mode)));


So that can reduce those 4 lines to 2. It may be desirable to break those up into multiple lines for readability.






share|improve this answer





















  • Thanks a lot. While the first bind method is more advanced, the second forEach answers my question. It looks more approachable.
    – kmonsoor
    May 30 at 19:34















up vote
1
down vote



accepted










Partial application



One way to simplify the code is to eliminate the lambda/anonymous functions using Function.bind() to create Partially applied functions. For instance:




router.get('/online-buyer-count', utils.mcache(15), (req, res, next) => 
realTime(DB.onlinebuyerNumQuery, 'onlinebuyerNum', req, res, next);
);



can be simplified to



router.get('/online-buyer-count', realTime.bind(null, DB.onlinebuyerNumQuery, 'onlinebuyerNum'));


And similarly for the other three routes, which reduces 12 lines to 4.



looping over the routes



The redundancy could also be simplified using Array.forEach()



['online', 'offline'].forEach(mode => 
['buyer', 'seller'].forEach(role =>
router.get('/'+mode+'-'+role+'-count', realTime.bind(null, DB[mode+role+'NumQuery'], mode+role+'Num'));
);
);


Taking that one step further using more partially-applied functions, the callback function of the inner forEach can be pulled out to a separate function:



const addRoute = (mode, role) => router.get('/'+mode+'-'+role+'-count', realTime.bind(null, DB[mode+role+'NumQuery'], mode+role+'Num'));


And then that function can be made into a partial and used within the nested forEach:



['online', 'offline'].forEach(mode => ['buyer', 'seller'].forEach(addRoute.bind(null, mode)));


So that can reduce those 4 lines to 2. It may be desirable to break those up into multiple lines for readability.






share|improve this answer





















  • Thanks a lot. While the first bind method is more advanced, the second forEach answers my question. It looks more approachable.
    – kmonsoor
    May 30 at 19:34













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Partial application



One way to simplify the code is to eliminate the lambda/anonymous functions using Function.bind() to create Partially applied functions. For instance:




router.get('/online-buyer-count', utils.mcache(15), (req, res, next) => 
realTime(DB.onlinebuyerNumQuery, 'onlinebuyerNum', req, res, next);
);



can be simplified to



router.get('/online-buyer-count', realTime.bind(null, DB.onlinebuyerNumQuery, 'onlinebuyerNum'));


And similarly for the other three routes, which reduces 12 lines to 4.



looping over the routes



The redundancy could also be simplified using Array.forEach()



['online', 'offline'].forEach(mode => 
['buyer', 'seller'].forEach(role =>
router.get('/'+mode+'-'+role+'-count', realTime.bind(null, DB[mode+role+'NumQuery'], mode+role+'Num'));
);
);


Taking that one step further using more partially-applied functions, the callback function of the inner forEach can be pulled out to a separate function:



const addRoute = (mode, role) => router.get('/'+mode+'-'+role+'-count', realTime.bind(null, DB[mode+role+'NumQuery'], mode+role+'Num'));


And then that function can be made into a partial and used within the nested forEach:



['online', 'offline'].forEach(mode => ['buyer', 'seller'].forEach(addRoute.bind(null, mode)));


So that can reduce those 4 lines to 2. It may be desirable to break those up into multiple lines for readability.






share|improve this answer













Partial application



One way to simplify the code is to eliminate the lambda/anonymous functions using Function.bind() to create Partially applied functions. For instance:




router.get('/online-buyer-count', utils.mcache(15), (req, res, next) => 
realTime(DB.onlinebuyerNumQuery, 'onlinebuyerNum', req, res, next);
);



can be simplified to



router.get('/online-buyer-count', realTime.bind(null, DB.onlinebuyerNumQuery, 'onlinebuyerNum'));


And similarly for the other three routes, which reduces 12 lines to 4.



looping over the routes



The redundancy could also be simplified using Array.forEach()



['online', 'offline'].forEach(mode => 
['buyer', 'seller'].forEach(role =>
router.get('/'+mode+'-'+role+'-count', realTime.bind(null, DB[mode+role+'NumQuery'], mode+role+'Num'));
);
);


Taking that one step further using more partially-applied functions, the callback function of the inner forEach can be pulled out to a separate function:



const addRoute = (mode, role) => router.get('/'+mode+'-'+role+'-count', realTime.bind(null, DB[mode+role+'NumQuery'], mode+role+'Num'));


And then that function can be made into a partial and used within the nested forEach:



['online', 'offline'].forEach(mode => ['buyer', 'seller'].forEach(addRoute.bind(null, mode)));


So that can reduce those 4 lines to 2. It may be desirable to break those up into multiple lines for readability.







share|improve this answer













share|improve this answer



share|improve this answer











answered May 30 at 16:50









Sam Onela

5,77461543




5,77461543











  • Thanks a lot. While the first bind method is more advanced, the second forEach answers my question. It looks more approachable.
    – kmonsoor
    May 30 at 19:34

















  • Thanks a lot. While the first bind method is more advanced, the second forEach answers my question. It looks more approachable.
    – kmonsoor
    May 30 at 19:34
















Thanks a lot. While the first bind method is more advanced, the second forEach answers my question. It looks more approachable.
– kmonsoor
May 30 at 19:34





Thanks a lot. While the first bind method is more advanced, the second forEach answers my question. It looks more approachable.
– kmonsoor
May 30 at 19:34













 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195196%2fnode-router-for-marketplace-routes%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Python Lists

Aion

JavaScript Array Iteration Methods