Code Monkey home page Code Monkey logo

Comments (4)

xiaojue avatar xiaojue commented on July 19, 2024

收到

from daruk.

xiaojue avatar xiaojue commented on July 19, 2024
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.

xiaojue avatar xiaojue commented on July 19, 2024

@whit15 看可以不可以给个最小 demo,我这边没有复现。

from daruk.

xiaojue avatar xiaojue commented on July 19, 2024

@whit15 也可以看置顶 issues,加我个人微信,我拽你进群

from daruk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.