简体   繁体   English

如何在 Express 中创建临时端点

[英]How Do I Create A Temporary Endpoint In Express

I'm using the node.js module express, and I want to create a temporary endpoint.我正在使用 node.js 模块 express,并且我想创建一个临时端点。 Either one that is removed once it is visited once, or one that I can manually remove.要么被访问一次就被删除,要么我可以手动删除。 Thanks in advance.提前致谢。

You would typically create some type of wildcard route that matches all of the temporary endpoints.您通常会创建某种类型的通配符路由来匹配所有临时端点。 Then, inside the route handler, you would check if this is still a valid temporary endpoint or not (by checking some data structure in memory or in a database).然后,在路由处理程序中,您将检查这是否仍然是一个有效的临时端点(通过检查内存或数据库中的某些数据结构)。

Then you aren't dynamically creating routes and removing routes.那么您就不会动态创建路由和删除路由。 Instead, you make one master route and use logic inside the route handler to decide if this particular temporary route is still valid or not?相反,您创建一个主路由并使用路由处理程序中的逻辑来决定这个特定的临时路由是否仍然有效?

Here's an example:下面是一个例子:

// single route handler for all temporary routes with one common prefix
app.get('/show/:id', (req, res) => {
   // dynamically check req.params.id to see if it's still a valid temporary route or not
   // by looking it up in some data structure (either in memory or in a database)
   // if it's valid, serve the content
   // if not valid return a 404 or some other appropriate error status
});

This is also a lot more scalable to millions of temporary endpoints and to clustered servers without burdening the routing system of the server.这对于数以百万计的临时端点和集群服务器来说也更具可扩展性,而不会增加服务器的路由系统的负担。

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

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