如何在 Express 的多个文件中包含路由处理程序?
在我的 NodeJS express
应用程序中,我有 app.js
,它有一些常见的路由。然后在 wf.js
文件中我想定义更多路由。
如何让 app.js
识别 wf.js
文件中定义的其他路由处理程序?
一个简单的 require 似乎不起作用。
In my NodeJS express
application I have app.js
that has a few common routes. Then in a wf.js
file I would like to define a few more routes.
How can I get app.js
to recognize other route handlers defined in wf.js
file?
A simple require does not seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
如果你想把路由放在一个单独的文件中,例如
routes.js
,你可以这样创建routes.js
文件:然后你可以要求它从
app.js
以这种方式传递app
对象:看看这些示例:https://github.com/visionmedia/express/tree/master/examples/route-separation
If you want to put the routes in a separate file, for example
routes.js
, you can create theroutes.js
file in this way:And then you can require it from
app.js
passing theapp
object in this way:Have a look at these examples: https://github.com/visionmedia/express/tree/master/examples/route-separation
在 Express 4.x 中,您可以获得路由器对象的实例并导入包含更多路由的另一个文件。您甚至可以递归地执行此操作,以便您的路由导入其他路由,从而允许您创建易于维护的 URL 路径。
例如,如果我的
/tests
端点已经有一个单独的路由文件,并且想要为/tests/automated
添加一组新的路由,我可能想打破这些/automated
路由到另一个文件,以保持我的/test
文件小且易于管理。它还允许您按 URL 路径将路由逻辑分组在一起,这非常方便。./app.js
的内容:./routes/tests.js
的内容:./routes/testRoutes/automated.js
的内容:In Express 4.x you can get an instance of the router object and import another file that contains more routes. You can even do this recursively so your routes import other routes allowing you to create easy-to-maintain URL paths.
For example, if I have a separate route file for my
/tests
endpoint already and want to add a new set of routes for/tests/automated
I may want to break these/automated
routes out into a another file to keep my/test
file small and easy to manage. It also lets you logically group routes together by URL path which can be really convenient.Contents of
./app.js
:Contents of
./routes/tests.js
:Contents of
./routes/testRoutes/automated.js
:在 @ShadowCloud 的示例的基础上,我能够动态地将所有路由包含在子目录中。
routes/index.js
然后将路由文件放入路由目录中,如下所示:
routes/test1.js
根据需要重复多次,最后放入 >app.js 放置
Building on @ShadowCloud 's example I was able to dynamically include all routes in a sub directory.
routes/index.js
Then placing route files in the routes directory like so:
routes/test1.js
Repeating that for as many times as I needed and then finally in app.js placing
如果您将 express-4.x 与 TypeScript 和 ES6 一起使用,这将是最好使用的模板:
src/api/login.ts
src/app.ts
比使用
var
和module.exports
干净得多。If you're using express-4.x with TypeScript and ES6, this would be the best template to use:
src/api/login.ts
src/app.ts
Much cleaner than using
var
andmodule.exports
.并在之前的答案的基础上进一步构建,此版本的routes/index.js将忽略任何不以.js(及其本身)结尾的文件
And build yet more on the previous answer, this version of routes/index.js will ignore any files not ending in .js (and itself)
/routes
文件夹中所有.js
文件的完全递归路由,将其放入app.js
中。在
/routes
中,放置whatevername.js
并初始化路由,如下所示:Full recursive routing of all
.js
files inside/routes
folder, put this inapp.js
.in
/routes
you putwhatevername.js
and initialize your routes like this:我正在尝试使用
"express": "^4.16.3"
更新此答案。这个答案与 ShortRound1911 的答案类似。server.js:
/src/routes/index.js:
/src/routes/siswa_route.js:
I am trying to update this answer with
"express": "^4.16.3"
. This answer is similar to the one from ShortRound1911.server.js:
/src/routes/index.js:
/src/routes/siswa_route.js:
如果您想要一个单独的 .js 文件来更好地组织您的路由,只需在
app.js
文件中创建一个变量,指向它在文件系统中的位置:然后,
其中
.foo
是在wf.js
文件中声明的一些函数。例如If you want a separate .js file to better organize your routes, just create a variable in the
app.js
file pointing to its location in the filesystem:then,
where
.foo
is some function declared in yourwf.js
file. e.g对所有这些答案的一个调整是:
只需使用正则表达式通过测试数组中的每个文件来进行过滤。它不是递归的,但它会过滤掉不以 .js 结尾的文件夹
One tweak to all of these answers:
Just use a regex to filter via testing each file in the array. It is not recursive, but it will filter out folders that don't end in .js
我知道这是一个老问题,但我试图为自己找出类似的问题,这就是我最终遇到的问题,所以我想将我的解决方案应用于类似的问题,以防其他人遇到与我相同的问题。我有。有一个很好的节点模块,名为 consign ,它可以完成许多可见的文件系统工作在这里为您提供(即 - 没有 readdirSync 的东西)。例如:
我有一个正在尝试构建的 Restful API 应用程序,我想将所有发送到 '/api/*' 的请求进行身份验证,并且我想将 api 中的所有路由存储到他们自己的目录(我们称之为“api”)。在应用程序的主要部分:
在路由目录内,我有一个名为“api”的目录和一个名为 api.js 的文件。在 api.js 中,我只是:
一切都按预期工作。希望这对某人有帮助。
I know this is an old question, but I was trying to figure out something like for myself and this is the place I ended up on, so I wanted to put my solution to a similar problem in case someone else has the same issues I'm having. There's a nice node module out there called consign that does a lot of the file system stuff that is seen here for you (ie - no readdirSync stuff). For example:
I have a restful API application I'm trying to build and I want to put all of the requests that go to '/api/*' to be authenticated and I want to store all of my routes that go in api into their own directory (let's just call it 'api'). In the main part of the app:
Inside of the routes directory, I have a directory called "api" and a file called api.js. In api.js, I simply have:
Everything worked as expected. Hope this helps someone.
index.js
路由/users.js
路由/books.js
如果你的服务器在本地运行(http://localhost:3000),那么
index.js
routes/users.js
routes/books.js
if you have your server running local (http://localhost:3000) then
我为此编写了一个小插件!厌倦了一遍又一遍地编写相同的代码。
https://www.npmjs.com/package/js-file-req
希望有帮助。
I wrote a small plugin for doing this! got sick of writing the same code over and over.
https://www.npmjs.com/package/js-file-req
Hope it helps.
您可以将所有路由功能放在其他文件(模块)中,并将其链接到主服务器文件。
在主express文件中,添加一个将模块链接到服务器的函数:
并为每个路由模型调用该函数:
在模块文件中(例如-login.js文件),照常定义函数:
并将其导出以请求方法作为键,值是一个对象数组,每个对象都有路径和功能键。
you can put all route functions in other files(modules) , and link it to the main server file.
in the main express file, add a function that will link the module to the server:
and call that function for each route model:
in the module files(for example - login.js file), define the functions as usual:
and export it with the request method as a key and the value is an array of objects, each with path and function keys.