简体   繁体   English

用户输入路线名称后如何动态创建快速路线

[英]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.我正在创建一个 covid 签到网站,我希望有人可以选择以企业主身份登录并为其业务创建签到路线。

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我需要这个选项,因为我不能只对每家公司进行硬编码,我希望创建一条 URL 路线,并以企业主输入的任何名称命名 -> 在字段中输入 Walmart 创建 /walmart.html

where walmart.html entails writing saying thankyou for checking in and displays how many people have checked in (have visited that route).其中 walmart.html 需要写下感谢您签到并显示有多少人签到(已访问该路线)。

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).您将需要一个数据库来存储所有自定义 URL,即一旦用户进入一个站点,将其存储在数据库中(以及有关用户的其他信息)。

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).在此示例中,假设您使用 MongoDB(使用 mongoose 库)和 EJS(只是示例实现,您不必使用这些)。

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):当用户发出请求时,您可以将所有请求定向到中间件(如果找到 URL,则发送文件,否则将 go 发送到默认主页):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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