Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 匹配API和同构路由_Node.js_Reactjs_Express_React Router - Fatal编程技术网

Node.js 匹配API和同构路由

Node.js 匹配API和同构路由,node.js,reactjs,express,react-router,Node.js,Reactjs,Express,React Router,我有一个在express上运行的同构应用程序: app.get('*', (req, res) => { match( { routes, location: req.url }, (error, redirectLocation, renderProps) => { ... } 目前我正在匹配通配符,因为这是我在大多数样板和教程中看到的方法 现在的问题是,我需要能够匹配特定的路由,例如/auth和/api,这些路由需要与同构应用程序的路由进行

我有一个在express上运行的同构应用程序:

  app.get('*', (req, res) => {
    match(
      { routes, location: req.url }, 
      (error, redirectLocation, renderProps) => { ... }
目前我正在匹配通配符,因为这是我在大多数样板和教程中看到的方法

现在的问题是,我需要能够匹配特定的路由,例如
/auth
/api
,这些路由需要与同构应用程序的路由进行不同的处理。i、 e.
/auth
路由需要对用户进行身份验证

我需要一种方法,能够指定这些路线,但仍然保持通配符匹配的路线使用我的同构应用程序

我考虑过不用通配符匹配器,我可以将同构应用程序的所有路由收集到一个数组中,并提供它来表示:

const isoRoutes = ['/home', '/about'];
app.get([isoRoutes], (req, res))

我不确定是否有更好的方法来实现这一点?

您可以这样编写代码:-

req.url.match(//regular expression);

Match()仅适用于正则表达式

如果您的
/auth
/api
路由没有为应用程序中的页面提供服务(即,您正在使用它们进行AJAX调用),则应该为它们提供单独的请求处理程序

只要在通配符
get
之前指定非通配符路由,就可以使用通配符

app.get('/auth', handleAuth);
app.get('/api', handleAPI);
app.get('*', handleRender);

您的意思是保持通配符匹配,并在回调中使用
req.url.match
来处理对
api
的请求?