Skip to content
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

add next() to express template, closes #1593 #1625

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cli/src/routeGeneration/templates/express.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export function RegisterRoutes(app: Router) {
validatedArgs,
successStatus: {{successStatus}},
});
next();
} catch (err) {
return next(err);
}
Expand Down
14 changes: 14 additions & 0 deletions tests/fixtures/express/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,21 @@ app.use((req: any, res: any, next: any) => {
req.stringValue = 'fancyStringForContext';
next();
});
let postRouteMiddlwareCalled = false;
RegisterRoutes(app);
app.get('/reset-post-route-middleware-status', (req, res, next) => {
postRouteMiddlwareCalled = false;
res.json({ postRouteMiddlwareCalled }).send();
next();
});
app.get('/post-route-middleware-status', (req, res, next) => {
res.json({ postRouteMiddlwareCalled }).send();
next();
});
app.use((req, res, next) => {
postRouteMiddlwareCalled = true;
next();
});

// It's important that this come after the main routes are registered
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
Expand Down
11 changes: 10 additions & 1 deletion tests/integration/express-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ import {
const basePath = '/v1';

describe('Express Server', () => {
it('can reset the post route middleware status', () => {
return verifyGetRequest('/reset-post-route-middleware-status', (_err, res) => {
expect(res.body.postRouteMiddlwareCalled).to.eq(false);
});
});
it('can handle get request to root controller`s path', () => {
return verifyGetRequest(basePath + '/', (_err, res) => {
const model = res.body as TestModel;
expect(model.id).to.equal(1);
});
});

it('can verify that post request middleware was called', () => {
return verifyGetRequest('/post-route-middleware-status', (_err, res) => {
expect(res.body.postRouteMiddlwareCalled).to.eq(true);
});
});
it('can handle get request to root controller`s method path', () => {
return verifyGetRequest(basePath + '/rootControllerMethodWithPath', (_err, res) => {
const model = res.body as TestModel;
Expand Down
Loading