In MEAN stack project there are 2 forms of Express functions here: one that takes a date parameter, and the other that doesn't, and the only difference is the 2nd URL component and aws method name.
-->How can I define 2 functions with all the code, that take the URL component as a parameter, construct the aws method name from that, and then return the Express function, thus reducing the code we need to maintain?
App.js file
API's which does not take date as parameter
const AWS = require("../db/data"),
aws = new AWS("aws_" + config.db.release.set);
router.get('/data/quern', (req, res) => {
aws.getdataquern()
.then(
record => { res.status(200).send(record) },
error => { res.status(500).send(error) });
});
router.get('/data/quern1', (req, res) => {
aws.getdataquern1()
.then(
record => { res.status(200).send(record) },
error => { res.status(500).send(error) });
});
router.get('/data/quern2', (req, res) => {
aws.getdataquern2()
.then(
record => { res.status(200).send(record) },
error => { res.status(500).send(error) });
});
Api's which takes date as parameter
router.get('/data/withDate/:date', (req, res) => {
aws.getdatawithDate(req.params.date)
.then(
record => { res.status(200).send(record) },
error => { res.status(500).send(error) });
});
router.get('/data/withDate1/:date', (req, res) => {
aws.getdatawithDate1(req.params.date)
.then(
record => { res.status(200).send(record) },
error => { res.status(500).send(error) });
});
router.get('/data/withDate2/:date', (req, res) => {
aws.getdatawithDat2(req.params.date)
.then(
record => { res.status(200).send(record) },
error => { res.status(500).send(error) });
});