简体   繁体   中英

How to dynamically create an express route once a user inputs the name of the route

this is gonna be tricky to explain but,

i am creating a covid check-in website and i would like the option for someone to login as a business owner and create a check-in route for their business.

So far I can do it by hardcoding a route for a business, for example the code for a McDonalds route is the following:

checkInCounter_1 = 0;
router.get('/mcdonalds.html', function(req, res, next) {
    checkInCounter_1 ++;
    res.send(`
    <!DOCTYPE html>
    <html>
    <head>
    </head>

    <body>
      <div id="header_maccas">
          <h1 id="headerCont3">Covid Check-In for McDonalds</h1>
      </div>
      <h1 id="maccas_thanks">thankyou for checking into McDonalds!</h1>
      <p>${checkInCounter_1} total people have checked in.</p>
    </body>
    </html>
    `);
});

but i really want the ability for a business owner to type in their business into the input field and then it dynamically creates a check-in route for their business - like the following:

"enter your business": the business owner then inputs Walmart or something and then a route pretty much exactly like the McDonalds one is created except its for Walmart.

i need this option as i cant just hardcode every single company, i want a URL route to be created and to be named whatever the business owner enters -> typing Walmart into the field creates /walmart.html

where walmart.html entails writing saying thankyou for checking in and displays how many people have checked in (have visited that route).

sorry for explaining poorly but i cant think of any other way to explain

thanks!

You will need a database to store all the custom URLs, ie once the user enters a site, store it in a database (as well as other information about the user).

In this example lets assume you use MongoDB (using the mongoose library) and EJS (just an example implementation, you don't have to use these).

When a user makes a request, you can direct all requests to a middleware (if URL is found, send a file, other wise, go to the default homepage):

app.use(async (req, res, next) => { // Whenever there's a request, it goes through this function
   var urlRequested = req.originalUrl; // req.originalUrl is everything after the name of your site. Beware it includes query strings (which can be easily removed with urlRequested.split('?')[0])
   // Here, you make a request to the database with urlRequested. If you have a URL that matches, you can send a result with information. It would be helpful to use a templating engine, such as EJS
   // Assuming you've connected and setup mongoose and EJS
   var isUrl = await nameOfYourMongooseModel.findOne({ storedUrl: urlRequested });
   if(isUrl !== null) {
      res.render('someEjsFile', { information: isUrl });
   } else {
      next(); // Go to your default homepage
   }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM