Node router for marketplace routes

Clash 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;
javascript object-oriented node.js ecmascript-6 url-routing
add a comment |Â
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;
javascript object-oriented node.js ecmascript-6 url-routing
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
add a comment |Â
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;
javascript object-oriented node.js ecmascript-6 url-routing
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;
javascript object-oriented node.js ecmascript-6 url-routing
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
add a comment |Â
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
add a comment |Â
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.
Thanks a lot. While the firstbindmethod is more advanced, the secondforEachanswers my question. It looks more approachable.
â kmonsoor
May 30 at 19:34
add a comment |Â
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.
Thanks a lot. While the firstbindmethod is more advanced, the secondforEachanswers my question. It looks more approachable.
â kmonsoor
May 30 at 19:34
add a comment |Â
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.
Thanks a lot. While the firstbindmethod is more advanced, the secondforEachanswers my question. It looks more approachable.
â kmonsoor
May 30 at 19:34
add a comment |Â
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.
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.
answered May 30 at 16:50
Sam Onela
5,77461543
5,77461543
Thanks a lot. While the firstbindmethod is more advanced, the secondforEachanswers my question. It looks more approachable.
â kmonsoor
May 30 at 19:34
add a comment |Â
Thanks a lot. While the firstbindmethod is more advanced, the secondforEachanswers 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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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