在Node项目中使用Express和Typescript,这将是express.Router的"最佳实践".
示例目录结构
|directory_name
---server.js
|--node_modules
|--routes
---index.ts
|--admin
---admin.ts
|--products
---products.ts
|--authentication
---authentication.ts
Run Code Online (Sandbox Code Playgroud)
所以在index.ts里面它会封装和管理所有的子路由器
//admin.ts (nested inside of index.ts)
import * as express from "express";
export = (() => {
let router = express.Router();
router.get('/admin', (req, res) => {
res.json({success: true});
});
return router;
})();
Run Code Online (Sandbox Code Playgroud)
//index.ts (master file for express.Router)
import * as express from "express";
//import sub-routers
import * as adminRouter from "./admin/admin";
import * as productRouter from "./products/products";
export = (() => {
let router = express.Router();
// …
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud) 在用 M-: (eval " PATH ")检查路径后,C:/Go/bin 的位置出现了,所以我知道找到了gofmt二进制文件。如果我尝试在其他文件上执行 Mx gofmt,它会给出正确的错误(不是 *.go 文件)。
但是我注意到消息中有一些奇怪的东西,这是 gofmt 试图格式化的地方..
调用 gofmt:gofmt (-wc:/Users/LunchBox/AppData/Local/Temp/gofmt5200q9o.go)
错误:(文件错误“搜索程序”“没有这样的文件或目录”“差异”)
为什么它在-w /appdata/locals/temp/etc ..?
不应该是我目前正在处理的文件吗?在 c:/work/users/ovRESTful/ovRESTful.go 中被称为 ovRESTful.go
我的代码行..
(add-hook 'before-save-hook 'gofmt-before-save)
Run Code Online (Sandbox Code Playgroud) 我在GIN库和Google文档中看到了一段GO代码,如下所示:
type (
T0 []string
T1 []string
T2 struct{ a, b int }
T3 struct{ a, c int }
T4 func(int, float64) *T0
T5 func(x int, y float64) *[]string
)
Run Code Online (Sandbox Code Playgroud)
我不明白的是,该分组在做什么,以及此实现的目的是什么(除非我错过了,否则讨论该主题的文档并不多)
杜松子酒图书馆的另一个例子
type (
RoutesInfo []RouteInfo
RouteInfo struct {
Method string
Path string
Handler string
}
Engine struct {
RouterGroup
HTMLRender render.HTMLRender
allNoRoute HandlersChain
allNoMethod HandlersChain
noRoute HandlersChain
noMethod HandlersChain
pool sync.Pool
trees methodTrees
RedirectTrailingSlash bool
RedirectFixedPath bool
HandleMethodNotAllowed bool
ForwardedByClientIP bool
}
)
Run Code Online (Sandbox Code Playgroud)
最后-抱歉,这是另一个主题,但与此主题相关
var _ IRouter = &Engine{} …
Run Code Online (Sandbox Code Playgroud)