Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using the same module for two endpoints

I’m working on an express js server for some whitelisting. I need to make two whitelists, but I don’t want to just make two files for them.

var whitelistOneRouter = require('./routes/whitelist')
var whitelistTwoRouter = require('./routes/whitelist')

app.use("/whitelists/whitelistOneRouter", whitelistOneRouter)
app.use("/whitelists/whitelistTwoRouter", whitelistTwoRouter)

whitelist.js

var router = express.Router();
var Merkle = require('../modules/merkle')

var merkleOne = new Merkle([])

router.get('/', function (req, res, next) {
    var address = req.body.address
    var proof = merkleOne.getProof(address)
    res.send(proof)
})
router.get('/root', function (req, res, next) {
    res.send(merkleOne.getRoot())
})
router.post('/new', function (req, res, next) {
    var whitelist = req.body.whitelist
    merkleOne.setNewWhitelist(whitelist)
    res.send(merkleOne.getRoot())
})

module.exports = router;

When I try to interact with one endpoint, it changes the other and vice versa. Does anyone know a better way to do this? I don’t want to make another file that’s the same code.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

OK, I’m guessing that you want a separate instance of your Merkle class for each.

Modules are cached so they only run their own initialization just once. So, the way you had it before, you had one router and one instance of your Merkle object. To have separate instances you move the code into a function so it can be called as many times as you want and will create a new Merkle object each time. Here’s one way to do that:

const Merkle = require('../modules/merkle');
const express = require('express');

module.exports = function() {
    const router = express.Router();
    const merkleOne = new Merkle([])

    router.get('/', function (req, res, next) {
        var address = req.body.address
        var proof = merkleOne.getProof(address)
        res.send(proof)
    })
    router.get('/root', function (req, res, next) {
        res.send(merkleOne.getRoot())
    })
    router.post('/new', function (req, res, next) {
        var whitelist = req.body.whitelist
        merkleOne.setNewWhitelist(whitelist)
        res.send(merkleOne.getRoot())
    })
    return router;
}

Then, use it like this:

const whiteListFn = require('./routes/whitelist');

app.use("/whitelists/whitelistOneRouter", whiteListFn());
app.use("/whitelists/whitelistTwoRouter", whiteListFn());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading