Code Monkey home page Code Monkey logo

Comments (8)

pleerock avatar pleerock commented on July 17, 2024 1

I tried, it's the same. And the normal html form doesn't json, it's not work too.
Besides, the request body is always undefined, how can I use middleware to deal with?

if you are working with json, you should always specify json content headers. btw are you using @JsonController or @Controller. Because if you are working NOT WITH JSON (forms are not working with json) you should use @Controller, not @JsonController.

export const app: Express.Application = createExpressServer({
controllerDirs: [__dirname + "/controller//*.controller.js"],
middlewareDirs: [__dirname + "/middleware/
/.middleware.js"],
interceptorDirs: [dirname + "/interceptor//
.interceptor.js"]
});
app.use(json());
app.use(urlencoded({extended: true}));

You are doing it wrong. createExpressServer creates and bootstraps express server. You probably should do you logic before it bootstrap the app. To do that you need to create your own express server and use useExpressServer instead. Something like this:

const app = express();
app.use(json());
app.use(urlencoded({extended: true}));
useExpressServer(app, {
    controllerDirs: [__dirname + "/controller/**/*.controller.js"],
    middlewareDirs: [__dirname + "/middleware/**/*.middleware.js"],
    interceptorDirs: [__dirname + "/interceptor/**/*.interceptor.js"]
});

from routing-controllers.

Diluka avatar Diluka commented on July 17, 2024 1

but I still have a question

when I use json params to post, how can I do pre-process in a middleware.
at the beginning, the json params successfully inject into the method params, but I can't get it from req.body

and I response json, but get any kind of params by middlewares translating into json to handle. which decorator shall I use, @Controller or @JsonController ?
Now I use @JsonController and body-parser. Is this right?

from routing-controllers.

Diluka avatar Diluka commented on July 17, 2024

full sample is here https://github.com/Diluka/leancloud-demo/tree/demo-post-body-is-empty

from routing-controllers.

pleerock avatar pleerock commented on July 17, 2024

did you set a Content-type: application/json in your request?

from routing-controllers.

Diluka avatar Diluka commented on July 17, 2024

I tried, it's the same. And the normal html form doesn't json, it's not work too.
Besides, the request body is always undefined, how can I use middleware to deal with?

from routing-controllers.

Diluka avatar Diluka commented on July 17, 2024

I solve this problem, but I consider it's maybe a bug.

this is not work:

export const app: Express.Application = createExpressServer({
    controllerDirs: [__dirname + "/controller/**/*.controller.js"],
    middlewareDirs: [__dirname + "/middleware/**/*.middleware.js"],
    interceptorDirs: [__dirname + "/interceptor/**/*.interceptor.js"]
});

app.use(json());
app.use(urlencoded({extended: true}));

this is work:

import {json, urlencoded} from "body-parser";
import {MiddlewareInterface, MiddlewareGlobalBefore} from "routing-controllers";

@MiddlewareGlobalBefore({priority: 2})
export class JsonBodyParserMiddleware implements MiddlewareInterface {
    use(request: any, response: any, next?: (err?: any) => any): any {
        return json()(request, response, next);
    }
}

@MiddlewareGlobalBefore({priority: 3})
export class UrlencodedBodyParserMiddleware implements MiddlewareInterface {
    use(request: any, response: any, next?: (err?: any) => any): any {
        return urlencoded({extended: true})(request, response, next);
    }
}

And the readme tells me to install body-parser, but it seems I shall use it by hard code

from routing-controllers.

Diluka avatar Diluka commented on July 17, 2024

thx, I get it.
I will use useExpressServer instead

from routing-controllers.

github-actions avatar github-actions commented on July 17, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

from routing-controllers.

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.