Comments (8)
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.
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.
full sample is here https://github.com/Diluka/leancloud-demo/tree/demo-post-body-is-empty
from routing-controllers.
did you set a Content-type: application/json in your request?
from routing-controllers.
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.
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.
thx, I get it.
I will use useExpressServer
instead
from routing-controllers.
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)
- question: building the code looks for Koa, while I'm only using Express HOT 8
- fix: class-validator does not work on last version of RC HOT 10
- fix: <Injected Response object is undefined> HOT 1
- question: @Get path with literal and variable HOT 3
- feature: <how make sub folder controllers> how make sub folder controllers HOT 2
- fix: jsdoc for Put http method, it mentions POST http method
- fix: TypeDI do not inject service HOT 1
- Annotate return type of `createExpressServer` with more narrow type HOT 1
- `cors: true` causes "Cannot set headers after they are sent to the client" error HOT 3
- fix: Global middlewares are not used if route prefix doesn't start with a slash ("/")
- fix: TypeError this.targetType.toLowerCase is not a function HOT 3
- fix: import statements should not affect the route matching order
- Can we create multiple paths for the same controller function? HOT 7
- fix: CJS being imported instead of ESM when trying to load controllers from directory import HOT 4
- feature: Allow to use `app.use(createExpressRouter())` instead of `useExpressServer(app)` HOT 6
- question: Routes with path parameters continue middleware chain (but those without, don't)? HOT 5
- feature: Dynamic route HOT 5
- feature: Introduce Generics to CurrentUserChecker for Enhanced Type Safety HOT 2
- fix: body in middleware is undefined. I have bodyParser HOT 3
- question: How to execute code after the response is sent to client
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 routing-controllers.