Hi,
When running the following two classes the Controller does not register. The server starts up and I can receive requests but I am unable to access my routes.
server.ts
/// <reference path='../typings/index.d.ts' />
import 'reflect-metadata';
import 'es6-shim';
import * as express from 'express';
import {useExpressServer} from 'routing-controllers';
import {createConnection, ConnectionOptions} from 'typeorm';
let bodyParser = require('body-parser');
let cookieParser = require('cookie-parser');
import session = require('express-session');
/**
* The server.
*
* @class Server
*/
class Server {
app: express.Application;
/**
* Bootstrap the application.
*
* @class Server
* @method bootstrap
* @static
*/
public static bootstrap(): Server {
return new Server();
}
/**
* Constructor.
*
* @class Server
* @constructor
*/
constructor() {
this.app = express();
this.createServer(this.app);
}
/**
* Configure application
*
* @class Server
* @method config
* @return void
*/
private createServer(app: express.Application): void {
/**************************************
* set up the server
**************************************/
// base directory. we use it because file in "required" in another module
const baseDir = __dirname;
useExpressServer(app, {
controllerDirs: [baseDir + "/server/controllers"],
// controllerDirs: [baseDir + "/server/controllers/**/*.js"],
// controllerDirs: [baseDir + "/server/controllers/**/*.controller.js"]
});
app.listen(process.env.PORT || 4200, function () {
console.log('&&&&&&& Listening on 4200 &&&&&&');
});
}
}
let server = Server.bootstrap();
module.exports = server.app;
This compiles OK and prints out
&&&&&&& Listening on 4200 &&&&&&
test.controller.ts
import {Response, Request, Router} from 'express';
import {Controller, Get, Req, Res, Post} from 'routing-controllers';
@Controller()
export class TestController {
constructor() {
console.log("@@@@@@@@@@@@@@@@@@ TestController @@@@@@@@@@@@@@@@@@")
}
@Get('/')
entry(@Req() request: Request, @Res() response: Response) {
console.log("*************** entry()")
}
}
Next, if I add require("./server/contollers/test.controller");
to the top of my server.ts the compiled code generates the following:
__decorate([
routing_controllers_1.Get('/'),
__param(0, routing_controllers_1.Req()),
__param(1, routing_controllers_1.Res()),
__metadata('design:type', Function),
__metadata('design:paramtypes', [express_1.Request, express_1.Response]),
__metadata('design:returntype', void 0)
This code gives:
/Users/ts/Documents/workspace/testapp/src/server/contollers/test.controller.js:207
__metadata('design:paramtypes', [express_1.Request, express_1.Response]),
^
ReferenceError: express_1 is not defined
at /Users/jt/Documents/workspace/testapp/src/server/contollers/test.controller.js:207:42
at Object.<anonymous> (/Users/jt/Documents/workspace/testapp/src/server/contollers/test.controller.js:319:2)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/jt/Documents/workspace/testapp/src/server.js:12:1)
tsconfig.json
{
"compilerOptions": {
"lib": ["es5", "es6", "dom"],
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": true,
"pretty": true,
"suppressImplicitAnyIndexErrors": true,
"typeRoots": [
"node_modules/@types"
],
"types": [
"typed-graphql"
]
},
"files": [
"./src/server.ts"
],
"exclude": [
"node_modules"
]
}
It largely follows the examples so I am stuck as to why it doesn't work. Is this an issue?
Cheers
JT