2.0.5 • Published 30 days ago
winkey-ngin v2.0.5
NGin 框架
一款轻量级的node框架,集成各种常用请求方法,让你书写api更加优雅、方便,更注重于业务逻辑的书写
初始化
import ngin, { HttpStatusCode } from "ngin";
const app = ngin();
app.listen();
Method
AsciiJSON
使用 AsciiJSON 生成具有转义的非 ASCII 字符的 ASCII-only JSON。
app.GET("/someJSON", function (context) {
const data = {
lang: "GO语言",
tag: "<br>",
};
// 输出 : {"lang":"GO\u8bed\u8a00","tag":"\u003cbr\u003e"}
context.AsciiJSON(HttpStatusCode.OK, data);
});
HTML 渲染
使用 LoadHTMLGlob(), 加载目标目录
app.LoadHTMLGlob("templates/*")
app.GET("/index", function(context) {
context.HTML(HttpStatusCode.OK, "index.html", gin.H{
"title": "Main website",
})
})
templates/index.tmpl
<html>
<h1>{{ .title }}</h1>
</html>
JSONP
使用 JSONP 向不同域的服务器请求数据。如果查询参数存在回调,则将回调添加到响应体中。
app.GET("/JSONP", function (context) {
const data = {
foo: "bar",
};
// /JSONP?callback=x
// 将输出:x({\"foo\":\"bar\"})
context.JSONP(HttpStatusCode.OK, data);
});
只绑定 url 查询字符串
app.GET("/query", function(context) {
// 或者简单地使用 ShouldBindQuery 方法自动绑定:
const form: {
user
} = {
user: null
password: null
}
// 在这种情况下,将自动选择合适的绑定
context.ShouldBindQuery(form)
if (form.user == "user" && form.password == "password") {
context.JSON(200, {"status": "you are logged in"})
} else {
context.JSON(401, {"status": "unauthorized"})
}
})
Multipart/Urlencoded 绑定
app.POST("/login", function(context) {
// 简单地使用 ShouldBind 方法自动绑定:
const form: {
user
} = {
user: null
password: null
}
// 在这种情况下,将自动选择合适的绑定
context.ShouldBind(form)
if (form.user == "user" && form.password == "password") {
context.JSON(200, {"status": "you are logged in"})
} else {
context.JSON(401, {"status": "unauthorized"})
}
})
Multipart/Urlencoded 表单
app.POST("/form_post", function (context) {
const message = context.PostForm("message");
const nick = context.DefaultPostForm("nick", "anonymous");
context.JSON(200, {
status: "posted",
message: message,
nick: nick,
});
});
Query 和 post form
app.POST("/post", function(context) {
// 获取query上的id值
const id = context.Query("id")
// 获取query上的page值,并且赋予默认值
const page = context.DefaultQuery("page", "0")
// 获取body上的name值
const name = context.PostForm("name")
// 获取body上的message值,并且赋予默认值
const message = context.DefaultPostForm("message", "1")
context.JSON(200)
})
JSON/String 渲染
app.GET("/json", function (context) {
context.JSON(200, {
name: "李白",
});
});
app.GET("/string", function (context) {
context.String(200, "HelloWorld");
});
Redirect
app.GET("/redirect", function (context) {
context.Redirect(302, "/new-url");
});
上传文件
app.POST("/upload", function(context) {
// 单文件
app.maxFilesSize = 1024 * 1024 2
// FormFile 只在form-data 下有用
file, _ := context.FormFile("file")
console.log(file.Filename)
const = "./" + file.Filename
// 上传文件至指定的完整文件路径
context.SaveUploadedFile(file, dst)
context.String(HttpStatusCode.OK, `${file.Filename} uploaded!`)
})
使用 HTTP 方法
app.GET("/someGet", getting);
app.POST("/somePost", posting);
app.PUT("/somePut", putting);
app.DELETE("/someDelete", deleting);
app.PATCH("/somePatch", patching);
app.HEAD("/someHead", head);
app.OPTIONS("/someOptions", options);
使用中间件
app.Use(logger())
...
function logger() {
return function(context) {
if (true) {
...
} else {
context.end('block')
// 返回false或者null均可拦截
return
}
}
}
Cookie 操作
import { CookieOptions } from 'ngin'
app.GET('/cookie', (ctx) => {
// 获取cookie中的某个key
const uid = ctx.Cookie('uid')
// 设置cookie options 可选
ctx.SetCookie('hi', 'bye', {}: CookieOptions)
ctx.String(200, "Hello World")
})
Binary 操作
app.POST("/binary", async (ctx) => {
// 可通过Binary获取二进制数据
const binary = await ctx.Binary();
ctx.String(200, "Hello World");
});
Context
导出的全局上下文
2.0.3
30 days ago
2.0.2
30 days ago
2.0.5
30 days ago
2.0.4
30 days ago
2.0.1
30 days ago
2.0.0
1 month ago
1.0.2
6 months ago
1.0.1
6 months ago
1.0.0
6 months ago
1.0.8
6 months ago
1.0.7
6 months ago
1.0.6
6 months ago
1.0.5
6 months ago
1.0.4
6 months ago
1.0.3
6 months ago
0.1.4
8 months ago
0.1.3
10 months ago
0.1.2
10 months ago
0.1.1
10 months ago
0.1.0
10 months ago