This commit is contained in:
oneao committed 2025-08-10 21:26:13 +08:00
1 parent dedefb78a7
commit 7c8e2906bc
19 files changed
+252

No files matched your search

@@ -0,0 +1,11 @@
package middleware
import (
"fmt"
"net/http"
)
// LoggingMiddleware logs incoming requests
func LoggingMiddleware(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Request received: %s %s\n", r.Method, r.URL.Path)
}
@@ -0,0 +1,98 @@
package router
import (
"net/http"
)
// HandlerFunc defines the signature of a handler function
type HandlerFunc func(w http.ResponseWriter, r *http.Request)
// Router struct holds all the routes, middlewares, and groups
type Router struct {
routes map[string]map[string]HandlerFunc
middleware []HandlerFunc
groups map[string]*Group
}
// NewRouter creates a new Router instance
func NewRouter() *Router {
return &Router{
routes: make(map[string]map[string]HandlerFunc),
groups: make(map[string]*Group),
}
}
// Use adds a global middleware to the router
func (r *Router) Use(middleware HandlerFunc) {
r.middleware = append(r.middleware, middleware)
}
// Handle registers a route with a method and a handler
func (r *Router) Handle(method, path string, handler HandlerFunc) {
if _, exists := r.routes[method]; !exists {
r.routes[method] = make(map[string]HandlerFunc)
}
r.routes[method][path] = handler
}
// Group creates a new group with a prefix
func (r *Router) Group(prefix string) *Group {
group := &Group{
router: r,
prefix: prefix,
}
r.groups[prefix] = group
return group
}
// Group struct represents a group of routes with a common prefix
type Group struct {
router *Router
prefix string
middleware []HandlerFunc
}
// Use adds a middleware to a group
func (g *Group) Use(middleware HandlerFunc) {
g.middleware = append(g.middleware, middleware)
}
// Handle registers a route in the group
func (g *Group) Handle(method, path string, handler HandlerFunc) {
fullPath := g.prefix + path
// Apply group middleware first
for _, m := range g.middleware {
g.router.Use(m)
}
// Register the route
g.router.Handle(method, fullPath, handler)
}
// Get registers a GET route in the group
func (g *Group) Get(path string, handler HandlerFunc) {
g.Handle("GET", path, handler)
}
// Post registers a POST route in the group
func (g *Group) Post(path string, handler HandlerFunc) {
g.Handle("POST", path, handler)
}
// ServeHTTP is the main entry point for handling HTTP requests
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// Apply global middlewares
for _, m := range r.middleware {
m(w, req)
}
// Route matching
if handlers, ok := r.routes[req.Method]; ok {
if handler, ok := handlers[req.URL.Path]; ok {
handler(w, req)
return
}
}
http.NotFound(w, req)
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 203 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 263 B

@@ -0,0 +1,143 @@
# **一、独立下载安装nodejs(若想切换使用多版本nodejs,请直接转至第二节)**
1、访问https://nodejs.org/en/地址,切换到download目录下的Prebuilt Installer的tab页,选择相应版本进行下载:
![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20240410163414144-427544868.png)
或者直接去列表里查找想要下载的版本:https://nodejs.org/dist/
![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20241018114323936-1844242583.png)
2、安装完成后,添加环境变量
![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20240410163841369-1133262585.png)
Pah中新建nodejs安装路径,如:D:\Program Files\node\nodejs
3、使用cmd命令,查看node版本
![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20240410164106670-257379519.png)
4、查看npm默认存放的位置
```
npm get prefix
npm get cache
```
![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20240410164727553-1497806551.png)
如果需要修改指定路径可以进行修改,若进行修改需要相应添加系统变量:
```
npm config set prefix "D:\..."
npm config set cache "D:\..."
```
# **二、安装nvm,并利用nvm安装管理多个nodejs版本(无需独立下载安装nodejs,若已安装需要先卸载)**
1、安装nvm
nvm是一个简单的bash脚本,它是用来管理系统中多个已存的Node.js版本
注:如果在安装nvm前已经安装了node,建议先将原来的node进行卸载或删除,可通过where node查看node安装路径:
![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815163105131-1420632637.png)
(1)下载并安装nvm,下载地址:https://github.com/coreybutler/nvm-windows/releases,下载.zip后缀的这个文件,下载后解压安装即可
![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815154226238-359528291.png)
(2)配置镜像源,从安装目录中找到settings.txt文件将一下配置复制进去
![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815154356500-781734063.png)
```
node_mirror: http://npmmirror.com/mirrors/node/
npm_mirror: http://registry.npmmirror.com/mirrors/npm/
```
否则执行nvm list available会报错:
![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20240410153748815-1410794935.png)
(3)查看安装的版本号,检查安装是否成功:nvm
![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815160406164-241997583.png)
(4)安装完后,通过环境变量可以查看到相关路径设置:
![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815160700818-1341311032.png)
2、使用nvm安装多版本nodejs
(1)查看nodejs所有版本
```
nvm list available
```
![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815160850386-1312464558.png)
(2)安装指定版本
```
nvm install 18.16.0
```
![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815161016883-1770065586.png)
(3)使用指定版本或切换到指定版本
```
nvm ues 18.16.0
```
![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815161656144-1744125380.png)
(4)查看当前版本信息
```
node -v (查看当前版本)
nvm list (查看已安装的所有nodejs版本)
```
![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815162127646-1370878516.png)
(5)卸载指定版本
```
nvm uninstall 18.16.0
```
(6)其他nvm常用命令
```
nvm off // 禁用node.js版本管理(不卸载任何东西)
nvm on // 启用node.js版本管理
nvm install <version> // 安装node.js的命名 version是版本号 例如:nvm install 8.12.0
nvm uninstall <version> // 卸载node.js是的命令,卸载指定版本的nodejs,当安装失败时卸载使用
nvm ls // 显示所有安装的node.js版本
nvm list available // 显示可以安装的所有node.js的版本
nvm use <version> // 切换到使用指定的nodejs版本
nvm v // 显示nvm版本
nvm install stable // 安装最新稳定版
```
3、遇到问题
在新机上安装nvm以及nodejs,安装完后 nvm ues 18.16.0 设置当前版本没有反应,
通过查看在安装nvm的时候,设置的nodejs路径下(settings.txt文件里的path值)没有相应文件夹,也就是系统变量下NVM_SYMLINK的路径文件夹不存在。
解决方案:下载任意版本nodejs安装到系统变量NVM_SYMLINK的路径文件夹下,然后再卸载掉,通过 nvm ues 18.16.0再切换到指定版本,可解决。