-
-
Notifications
You must be signed in to change notification settings - Fork 286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v6 to v7 router migration #2368
Comments
Hello @MatejSkrbis
yes it was always the case in v6 and it's the same thing in v7. Controller is a provider like a Service is it. So if you read the code about the Server lifecycle here (https://tsed.io/docs/hooks.html#introduction), all providers are created during the injector.load() step. There is a special steps when the routers/controllers are attached to the Express/Koa app (mount all controllers). But the different in v7 is the router itself. Instead of provide an Express/Koa router as injectable service, Ts.ED provide it's own Router abstraction. This router as the same methods provided by an ExpressRouter. The router store all method/handler created manually and then there information are added to the app. This part works as expected, but maybe you have an usage that is different and not covered by my integration test. I don't use often this approach (excepted for Ts.ED Module, but isn't a controller). See you |
I am not sure what is wrong with the example I provided. In Server.ts there is a line of code (line 51)
This does not registers |
Please create a repository example! |
I've added the example to the repository: Can you also please update the instructions it must be in a repository instead of copy/pasted code? Thanks. |
Ho sorry I haven't seen the Zip. My bad :D |
But in your case, isn't enough ;) |
Hoo ok, it's clear now! Just use the this.app.get('/schema') if you add routes on server level. |
Thank you, but it seems the problem is the same as with |
import {join} from "path";
import {Configuration, Inject} from "@tsed/di";
import {PlatformApplication, PlatformRouter} from "@tsed/common";
import "@tsed/platform-express"; // /!\ keep this import
import "@tsed/ajv";
import {config} from "./config/index";
import * as rest from "./controllers/rest/index";
@Configuration({
...config,
acceptMimes: ["application/json"],
httpPort: process.env.PORT || 8083,
httpsPort: false, // CHANGE
disableComponentsScan: true,
mount: {
"/rest": [
...Object.values(rest)
]
},
middlewares: [
"cors",
"cookie-parser",
"compression",
"method-override",
"json-parser",
{ use: "urlencoded-parser", options: { extended: true }}
],
views: {
root: join(process.cwd(), "../views"),
extensions: {
ejs: "ejs"
}
},
exclude: [
"**/*.spec.ts"
]
})
export class Server {
@Inject()
protected app: PlatformApplication;
@Configuration()
protected settings: Configuration;
public $beforeRoutesInit(): void {
console.log('Before routes init');
this.app.use("/schema", async(req: any, res: any) => {
return res.send('This is my schema');
});
}
} works for me :) Note: You can find more details on PlatformRouter as here: https://www.npmjs.com/package/@tsed/platform-router |
Thank you! |
You're welcome :) |
@MatejSkrbis Don't forget to add a star on Github project if you have time ;) |
Information
I am trying to migrate from TsED v6 to v7. I added one route
/schemas
on Server configuration and included that as a module in package.json file. I usedthis.app.rawRouter.get('/schemas', this.myMethod)
to do it. After migration I tried to use injected PlatformRouter to do the same, but the endpoint no longer registers.There is no error, it just doesn't work. If the logic is moved into the controller constructor, then it works and new endpoint is registered successfully. The problem is I am using this as a module for many different projects, and would like to register this endpoint before any other controllers. I am not sure if I am doing something wrong.
Also I noticed that
$beforeRoutesInit
in server is executed after the controller is already initialized. Is this expected behaviour?Example.zip
The text was updated successfully, but these errors were encountered: