Routing-controllers: why will method be executed when the previous method return a result?

Created on 17 Aug 2016  路  9Comments  路  Source: typestack/routing-controllers

when I visit: http://localhost:3000/api/user/all?id=123
the result is '{"id":"all","flag":"getOne"}',but there is a Error: Can't set headers after they are sent.
why will Method:getAll be executed when Method:getOne return a result?

import {Controller, JsonResponse, Param, Get, QueryParam} from "routing-controllers";

@Controller('/user')
export class UserController {

    @Get("/:id")
    @JsonResponse()
    getOne(@Param("id") id:string) {
        console.log('getOne');
        return {id:id,flag:'getOne'};
    }

    @Get("/all")
    getAll(@QueryParam("id") id:number) {
        console.log('getAll');
        return {id:id,flag:'getAll'};
    }
}

Most helpful comment

Now, I understand the design. thx
I previously use express not in the proper way.
It needs to execute middlewares in the both sides of route.

All 9 comments

because both your actions satisfy executed route.

when you access user/all it satisfies for both /:id and all. How can id know if your "all" isn't id?

You can solve it either by using another route for all, either to specify regexp to "id" what it can be, and what it can't. For example:
/users/:id(\\d+)" if id can be only a number

I know both of them match the path, but when the first returned why the second still process.

here is my error print:

getAll
[2016-08-17 14:34:15.362] [ERROR] app.js - Promise娌℃湁鎷掔粷澶勭悊鍑芥暟:  Promise {
  <rejected> Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11)
    at ServerResponse.header (D:\Workspace\ProjectSpace\lp-test\node_modules\express\lib\response.js:719:10)
    at ServerResponse.json (D:\Workspace\ProjectSpace\lp-test\node_modules\express\lib\response.js:247:10)
    at ExpressDriver.handleError (D:\Workspace\ProjectSpace\lp-test\node_modules\routing-controllers\driver\ExpressDriver.js:248:22)
    at D:\Workspace\ProjectSpace\lp-test\node_modules\routing-controllers\RoutingControllerExecutor.js:110:67
    at process._tickCallback (internal/process/next_tick.js:103:7) }  鍘熷洜:  Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11)
    at ServerResponse.header (D:\Workspace\ProjectSpace\lp-test\node_modules\express\lib\response.js:719:10)
    at ServerResponse.json (D:\Workspace\ProjectSpace\lp-test\node_modules\express\lib\response.js:247:10)
    at ExpressDriver.handleError (D:\Workspace\ProjectSpace\lp-test\node_modules\routing-controllers\driver\ExpressDriver.js:248:22)
    at D:\Workspace\ProjectSpace\lp-test\node_modules\routing-controllers\RoutingControllerExecutor.js:110:67
    at process._tickCallback (internal/process/next_tick.js:103:7)

Because there is no logic that prevents execution of the second action if first returned something. Not sure if it should be there.

In your case you should have proper defined routes

In your framework

                if (action.isJsonTyped) {
                    response.json(result);
                }
                else {
                    response.send(String(result));
                }
                options.next();

why call next() after response.send()? Doing so will cause error

because there can be a middleware that should be called after route execution (for example middleware that logs number of seconds spent on loading). Without calling next() it will not execute such middlewares

And there can be some middlewares just deal with errors like 404 NOT FOUND. Like this, the middleware is always do the error handle, although there is no error need to be handled

didnt get what you mean. Error handlers works different way, for error middleware you call next(err) and thats is called only when error occur, not always like in this case

Now, I understand the design. thx
I previously use express not in the proper way.
It needs to execute middlewares in the both sides of route.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

posabsolute picture posabsolute  路  4Comments

azaslonov picture azaslonov  路  5Comments

circy picture circy  路  3Comments

ghost picture ghost  路  5Comments

iangregsondev picture iangregsondev  路  3Comments