Comments (4)
收到
from daruk.
import {
Daruk,
DarukContext,
defineMiddleware,
MiddlewareClass,
DarukServer,
controller,
middleware,
post,
validate,
Next,
} from "daruk";
(async () => {
const myapp = DarukServer();
@defineMiddleware("cors")
class Cors implements MiddlewareClass {
public initMiddleware(daruk: Daruk) {
return async (ctx: DarukContext, next: Next) => {
ctx.set("Access-Control-Allow-Origin", "*");
ctx.set(
"Access-Control-Allow-Methods",
"GET,POST,OPTIONS,PUT,PATCH,DELETE"
);
ctx.set(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization, x-access-token"
);
await next();
};
}
}
@controller()
class AuthController {
@middleware("cors")
@post("/admin/login")
@validate({
user_id: {
type: "string",
required: true,
},
})
public async login(ctx: DarukContext, next: Next) {
ctx.body = ctx.request.body.user_id;
}
}
await myapp.binding();
myapp.listen(3000);
})();
curl -X POST -d "user_id=abc" http://localhost:3000/admin/login -v
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3000 (#0)
> POST /admin/login HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Length: 11
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 11 out of 11 bytes
< HTTP/1.1 200 OK
< X-Request-Id: 8974cef3-6157-496e-a562-d16c79d1fd38
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET,POST,OPTIONS,PUT,PATCH,DELETE
< Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization, x-access-token
< Content-Type: text/plain; charset=utf-8
< Content-Length: 3
< Date: Wed, 27 Oct 2021 08:48:14 GMT
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
abc
from daruk.
@whit15 看可以不可以给个最小 demo,我这边没有复现。
from daruk.
@whit15 也可以看置顶 issues,加我个人微信,我拽你进群
from daruk.
Related Issues (20)
- 文档不完善 HOT 7
- 建议更换koa-router为@koa/router HOT 3
- 这个项目是否有迭代计划和 TODO? HOT 2
- 安装 | Daruk HOT 5
- 介绍 | Daruk HOT 9
- 项目启动 | Daruk HOT 4
- 装饰器 | Daruk HOT 3
- 生命周期 | Daruk
- 部署 | Daruk HOT 2
- 调用链性能追踪 | Daruk
- controller 装饰器的参数类型声明漏咯 HOT 5
- 如何在timer里面读取provide注册的类 HOT 1
- 想实现 @Param 但是,与部分装饰器冲突,导致无法获取参数内容 HOT 1
- 文件既然已经确定了,直接 require 性能应该会更好 HOT 1
- 组建一个真实用户群 HOT 1
- How to get url params HOT 1
- 希望能完善一下文档 HOT 1
- 设置 koa-session 时提示需要 keys
- Enable strict type-checking HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from daruk.