in Education by
I want to reuse a code snippet which will save me A WHOLE LOT OF TIME. I want to make POST, DELETE, PATCH and GET requests modular. I have a js File which defines the basic route for each module (mod) and since I'm using 23 modules, which will all function the same way, I'd like to take this shortcut. Heres the "Basic Route File" let route = ""; let mod = undefined; router.get("/" + route, verify, async (req, res) => { const id = req.query.id; let data; if (id) { data = await mod.findOne({_id: id}); } else { data = await mod.find({}); } if (data) return res.status(200).json({status: 404, message: "The data can't be found!", data: []}); else return res.status(200).json({status: 200, message: "Found data!", data: data}); }); router.post("/" + route, verify, async (req, res) => { let data = new mod(req.body); data = await data.save(); if (data) return res.status(200).json({status: 404, message: "The data can't be found!", data: []}); else return res.status(200).json({status: 200, message: "Found data!", data: data}); }); router.patch("/" + route, verify, async (req, res) => { const id = req.query.id; let data; if (id) { data = await mod.updateOne({_id: id}, {$set: req.body}); } if (!data) return res.status(200).json({status: 404, message: "The data can't be found!", data: []}); else return res.status(200).json({status: 200, message: "Found data and updated!", data: data}); }); router.delete("/" + route, verify, async (req, res) => { const id = req.query.id; let data; if (id) { data = await mod.deleteOne({_id: id}); } if (!data) return res.status(200).json({status: 404, message: "The data can't be found!", data: []}); else return res.status(200).json({status: 200, message: "Found data and deleted!", data: data}); }); module.exports = function(proute, pmodule){ route = proute; module = pmodule; return router; }; And in one of the other router files I tell each route which module they are using and what they are called. router.use(yukir("disc-server", DiscServer)); router.use(yukir("disc-user", User)); router.use(yukir("autochannel", AutoChannel)); The thing is I don't get any errors but a 404 error so the route can't be found, which is really strange. Can someone help me with that? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Your problem is that strings in Javascript are pass by value, not pass by reference, so at the time the calls to the router methods are made, route and mod are the empty string and undefined respectively. The router functions thus always register the route at "/". Wrap your route definition code inside the factory function and you'll be fine.

Related questions

0 votes
    here is my code. var http=require("http"); var fs = require("fs"); var express = require(" ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    When I run a new page, I must specify size of the viewport using the setViewport function: await page ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I want to build my js/css code, write it on disk and serve it using webpack-dev-server in a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I am using the electron-quick-start app with a windows machine. I have added logging statements in the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    I am using the electron-quick-start app with a windows machine. I have added logging statements in the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I am using react FineUploaderS3, with params: this.myUploader = new FineUploaderS3({ options: { request: { ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I am trying to create a simple node.js app on heroku. Here is app.js: console.log("Starting App" ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I have a particular context in which one data are transformed a lot to get transferred across network. At ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have a problem, I want to move value of Object[0] nazwa, which is a result of query (let's ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have a problem, I want to move value of Object[0] nazwa, which is a result of query (let's ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I copied and pasted a for-await-of example from MDN and still get an error telling me await is a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I copied and pasted a for-await-of example from MDN and still get an error telling me await is a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    Is it possible to add a property (with get and set method) to the scope of a file without making ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    I have been implementing a Next.js app for a side project of mine. It is a basic brochure-style ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I created a REST api and want to validate the body and the params before calling the controller logic. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
...