diff --git a/examples/ts/express/mysql/app.ts b/examples/ts/express/mysql/app.ts index 44b749c..b90b2d2 100644 --- a/examples/ts/express/mysql/app.ts +++ b/examples/ts/express/mysql/app.ts @@ -2,6 +2,7 @@ import express from 'express' import mysql from 'mysql' const routes = require('./routes') +const custom_routes = require('./custom_routes') const app = express() app.use(express.json()) @@ -34,6 +35,7 @@ const pool = mysql.createPool({ }) routes.register(app, pool) +custom_routes.register(app, pool) const port = process.env.PORT || 3000 app.listen(port, () => { diff --git a/examples/ts/express/mysql/custom_routes.ts b/examples/ts/express/mysql/custom_routes.ts new file mode 100644 index 0000000..b175712 --- /dev/null +++ b/examples/ts/express/mysql/custom_routes.ts @@ -0,0 +1,10 @@ +import { Express, NextFunction, Request, Response } from 'express' +import { Pool } from 'mysql' + +exports.register = (app: Express, pool: Pool) => { + + app.get('/custom/route', (req: Request, res: Response, next: NextFunction) => { + res.json({ "custom": true }) + }) + +} diff --git a/src/templates/app.ts.ejs b/src/templates/app.ts.ejs index 24289b4..ec5fced 100644 --- a/src/templates/app.ts.ejs +++ b/src/templates/app.ts.ejs @@ -1,7 +1,18 @@ +<% +// "custom_routes.ts" => "custom_routes" +function removeExtension(filename) { + return filename.replace(/\.ts$/, '') +} +-%> import express from 'express' import mysql from 'mysql' const routes = require('./routes') +<% customRouteFilenames.forEach(filename => { + const routerName = removeExtension(filename) +-%> +const <%- routerName %> = require('./<%- routerName %>') +<% }) -%> const app = express() app.use(express.json()) @@ -36,6 +47,11 @@ const pool = mysql.createPool({ }) routes.register(app, pool) +<% customRouteFilenames.forEach(filename => { + const routerName = removeExtension(filename) +-%> +<%- routerName %>.register(app, pool) +<% }) -%> const port = process.env.PORT || 3000 app.listen(port, () => {