0

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) });
});


1 Answer 1

1

You can use one common function for all of them where you pass the aws method you want to call as a string and you pass any argument you want to pass to that function. Also, note that .status(200) is not necessary as that's already the default status.

const AWS = require("../db/data"),
    aws = new AWS("aws_" + config.db.release.set);

function handleResponse(res, awsFnName, arg) {
    aws[awsFnName](arg).then(
        record => { res.send(record) },
        error => { res.status(500).send(error) }
    );
}

router.get('/data/quern', (req, res) => {
    handleResponse(res, "getdataquern");
});

router.get('/data/quern1', (req, res) => {
    handleResponse(res, "getdataquern1");
});

router.get('/data/quern2', (req, res) => {
    handleResponse(res, "getdataquern2");
});

router.get('/data/withDate/:date', (req, res) => {
    handleResponse(res, "getdatawithDate", req.params.date)
});

router.get('/data/withDate1/:date', (req, res) => {
    handleResponse(res, "getdatawithDate1", req.params.date)
});

router.get('/data/withDate2/:date', (req, res) => {
    handleResponse(res, "getdatawithDate2", req.params.date)
});

Note, if the route declarations themselves were really this repetitive, they could be table driven where you generate all the routes in one loop by iterating a table of parameters. Here's how the routes themselves could be generated:

const AWS = require("../db/data"),
    aws = new AWS("aws_" + config.db.release.set);

function handleResponse(res, awsFnName, arg) {
    aws[awsFnName](arg).then(
        record => { res.send(record) },
        error => { res.status(500).send(error) }
    );
}

["quern", "quern1", "quern2"].forEach(suffix => {
    router.get(`/data/${suffix}`, (req, res) => {
        handleResponse(res, `getdata${suffix}`);
    });
});

["withDate", "withDate1", "withDate2"].forEach(suffix => {
    router.get(`/data/${suffix}/:date`, (req, res) => {
        handleResponse(res, `getdata${suffix}`, req.params.date);
    });
});

This code generates all six routes.


I'm not always a huge fan of generated routes because it's not nearly as clear to a reader of the code exactly what routes this code is registering. Obviously, it can be figured out by studying the code, but there is some maintenance advantage to the first method above, even though it has a little more redundancy in the code.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks this worked, can I optimize these further with routes as you mentioned in the Note?
@DharmishaDoshi - Look at my latest edit.
Hi @jfriend00, does this looks optimized stackblitz.com/edit/js-qvuabz. However this only works with API without dates

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.