Skip to content

Commit 584fb0d

Browse files
author
hirsch88
committed
add new core structure
1 parent f20e8a7 commit 584fb0d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+445
-386
lines changed

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "express-typescript-boilerplate",
33
"version": "1.7.0",
44
"description": "A delightful way to building a RESTful API with NodeJs & TypeScript",
5-
"main": "src/index.ts",
5+
"main": "src/app.ts",
66
"scripts": {
77
"test": "NODE_ENV=test ./node_modules/.bin/jest ./test/unit",
88
"test:pretty": "NODE_ENV=test ./node_modules/.bin/jest ./test/unit --verbose",
@@ -24,7 +24,7 @@
2424
"console:dev": "./node_modules/.bin/ts-node ./src/console/commander.ts",
2525
"console:help": "./node_modules/.bin/ts-node --fast ./src/console/commander.ts --help",
2626
"serve": "./node_modules/.bin/nodemon --watch 'src/**/*.ts' --watch 'src/**/*.json' --watch '.env'",
27-
"start": "node dist/index.js"
27+
"start": "node dist/app.js"
2828
},
2929
"repository": "git+ssh://git@github.com/w3tec/express-typescript-boilerplate.git",
3030
"engines": {
@@ -57,6 +57,7 @@
5757
"@types/bluebird": "^3.5.4",
5858
"@types/body-parser": "^1.16.3",
5959
"@types/bookshelf": "^0.9.1",
60+
"@types/chalk": "^0.4.31",
6061
"@types/commander": "^2.9.0",
6162
"@types/cors": "^2.8.1",
6263
"@types/debug": "0.0.29",
@@ -82,6 +83,7 @@
8283
"body-parser": "^1.17.2",
8384
"bookshelf": "^0.10.3",
8485
"bookshelf-camelcase": "^1.1.4",
86+
"chalk": "^1.1.3",
8587
"class-validator": "^0.7.0",
8688
"commander": "^2.9.0",
8789
"compression": "^1.6.2",
@@ -92,6 +94,7 @@
9294
"express": "^4.15.3",
9395
"express-status-monitor": "^0.1.9",
9496
"faker": "^4.1.0",
97+
"figlet": "^1.2.0",
9598
"glob": "^7.1.2",
9699
"handlebars": "^4.0.10",
97100
"helmet": "^3.6.1",

src/api/controllers/UserController.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ import { Controller, Get, Post, Put, Delete, RequestParam, RequestBody, Response
1111
import { myExpress } from 'my-express';
1212
import { UserService } from '../services/UserService';
1313
import { Types } from '../../constants/Types';
14-
import { Service, Middleware } from '../../constants/Targets';
15-
import { AuthenticateMiddleware } from '../middlewares/AuthenticateMiddleware';
16-
import { PopulateUserMiddleware } from '../middlewares/PopulateUserMiddleware';
17-
import { ioc } from '../../core/IoC';
14+
import { Service } from '../../constants/Targets';
15+
// import { AuthenticateMiddleware } from '../middlewares/AuthenticateMiddleware';
16+
// import { PopulateUserMiddleware } from '../middlewares/PopulateUserMiddleware';
17+
// import { ioc } from '../../core/IoC';
1818

1919
// Get middlewares
20-
const authenticate = ioc.Container.getNamed<AuthenticateMiddleware>(Types.Middleware, Middleware.AuthenticateMiddleware);
21-
const populateUser = ioc.Container.getNamed<PopulateUserMiddleware>(Types.Middleware, Middleware.PopulateUserMiddleware);
20+
// const authenticate = ioc.Container.getNamed<AuthenticateMiddleware>(Types.Middleware, Middleware.AuthenticateMiddleware);
21+
// const populateUser = ioc.Container.getNamed<PopulateUserMiddleware>(Types.Middleware, Middleware.PopulateUserMiddleware);
2222

23-
24-
@Controller('/users', authenticate.use)
23+
// authenticate.use
24+
@Controller('/users')
2525
export class UserController {
2626

2727
constructor( @inject(Types.Service) @named(Service.UserService) private userService: UserService) { }
@@ -38,7 +38,8 @@ export class UserController {
3838
return res.created(user.toJSON());
3939
}
4040

41-
@Get('/me', populateUser.use)
41+
// populateUser.use
42+
@Get('/me')
4243
public async findMe( @Request() req: myExpress.Request, @Response() res: myExpress.Response): Promise<any> {
4344
return res.found(req.user);
4445
}

src/api/middlewares/AuthenticateMiddleware.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export class AuthenticateMiddleware {
1919
this.log = new Logger('api:middleware:AuthenticateMiddleware');
2020
}
2121

22-
2322
public use = (req: myExpress.Request, res: myExpress.Response, next: myExpress.NextFunction): void => {
2423
const token = this.getToken(req);
2524

src/api/middlewares/PopulateUserMiddleware.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export class PopulateUserMiddleware {
1818
this.log = new Logger('api:middleware:PopulateUserMiddleware');
1919
}
2020

21-
2221
public use = (req: myExpress.Request, res: myExpress.Response, next: myExpress.NextFunction): void => {
2322
// Check if the authenticate middleware was successful
2423
if (!req.tokeninfo || !req.tokeninfo.user_id) {

src/app.ts

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,34 @@
11
/**
2-
* APPLICATION CONFIGURATION
2+
* EXPRESS TYPESCRIPT BOILERPLATE
33
* ----------------------------------------
44
*
5-
* This is the place to add any other express module and register
6-
* all your custom middlewares and routes.
5+
* Gery Hirscheld<@hirsch88>
6+
*
7+
* This is a boilerplate for Node.js Application written in TypeScript.
8+
* The basic layer of this app is express. For further information visit
9+
* the 'README.md' file.
10+
*
11+
* It is very important the '/core' module is loaded first, because this
12+
* module loads all essentials third-party-libs and our configs we need.
13+
*
14+
* To add express modules go to the 'app.ts' file. All the IOC registrations
15+
* are in the 'container.ts' file.
716
*/
817

9-
import * as path from 'path';
10-
import * as cors from 'cors';
11-
import * as morgan from 'morgan';
12-
import * as helmet from 'helmet';
13-
import * as express from 'express';
14-
import * as favicon from 'serve-favicon';
15-
import * as bodyParser from 'body-parser';
16-
import * as compression from 'compression';
17-
import { bootstrap } from './core/Bootstrap';
18-
import { Log } from './core/log';
19-
20-
21-
bootstrap.configureExpress((app: express.Application) => app
22-
23-
// Enabling the cors headers
24-
.options('*', cors())
25-
.use(cors())
26-
27-
// Helmet helps you secure your Express apps by setting various HTTP headers. It's not a silver bullet, but it can help!
28-
.use(helmet())
29-
.use(helmet.noCache())
30-
.use(helmet.hsts({
31-
maxAge: 31536000,
32-
includeSubdomains: true
33-
}))
18+
// Helps to add metadata to classes with annotations
19+
import 'reflect-metadata';
3420

35-
// Compress response bodies for all request that traverse through the middleware
36-
.use(compression())
21+
// Defines the main dependencies and returns the
22+
// bootstrap instance to get the server started.
23+
import { App } from './core/App';
24+
import { CustomConfig } from './config/CustomConfig';
3725

38-
// Parse incoming request bodies in a middleware before your handlers, available under the req.body property.
39-
.use(bodyParser.json())
40-
.use(bodyParser.urlencoded({
41-
extended: true
42-
}))
26+
export const app = new App([
4327

44-
// Serve static filles like images from the public folder
45-
.use(express.static(path.join(__dirname, 'public'), { maxAge: 31557600000 }))
28+
// Here you can add more custom configurations
29+
new CustomConfig()
4630

47-
// A favicon is a visual cue that client software, like browsers, use to identify a site
48-
.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
31+
]);
4932

50-
// HTTP request logger middleware for node.js
51-
.use(morgan('dev', {
52-
stream: {
53-
write: Log.info
54-
}
55-
}))
56-
);
33+
// Launch the server with all his awesome features.
34+
app.main();

src/config/AppConfig.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* APPLICATION CONFIGURATION
3+
* ----------------------------------------
4+
*
5+
* This is the place to add any other express module and register
6+
* all your custom middlewares and routes.
7+
*/
8+
9+
import * as path from 'path';
10+
import * as cors from 'cors';
11+
import * as morgan from 'morgan';
12+
import * as helmet from 'helmet';
13+
import * as express from 'express';
14+
import * as favicon from 'serve-favicon';
15+
import * as bodyParser from 'body-parser';
16+
import * as compression from 'compression';
17+
import { Log } from '../core/log';
18+
import { App, Configurable } from '../core/App';
19+
20+
21+
export class AppConfig implements Configurable {
22+
public configure(app: App): void {
23+
app.Express
24+
// Enabling the cors headers
25+
.options('*', cors())
26+
.use(cors())
27+
28+
// Helmet helps you secure your Express apps by setting various HTTP headers. It's not a silver bullet, but it can help!
29+
.use(helmet())
30+
.use(helmet.noCache())
31+
.use(helmet.hsts({
32+
maxAge: 31536000,
33+
includeSubdomains: true
34+
}))
35+
36+
// Compress response bodies for all request that traverse through the middleware
37+
.use(compression())
38+
39+
// Parse incoming request bodies in a middleware before your handlers, available under the req.body property.
40+
.use(bodyParser.json())
41+
.use(bodyParser.urlencoded({
42+
extended: true
43+
}))
44+
45+
// Serve static filles like images from the public folder
46+
.use(express.static(path.join(__dirname, '..', 'public'), { maxAge: 31557600000 }))
47+
48+
// A favicon is a visual cue that client software, like browsers, use to identify a site
49+
.use(favicon(path.join(__dirname, '..', 'public', 'favicon.ico')))
50+
51+
// HTTP request logger middleware for node.js
52+
.use(morgan('dev', {
53+
stream: {
54+
write: Log.info
55+
}
56+
}));
57+
}
58+
}

src/config/CustomConfig.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* config.Custom
3+
* ------------------------------------
4+
*
5+
* Define all log adapters for this application and chose one.
6+
*/
7+
8+
import { Log } from '../core/log';
9+
import { App, Configurable } from '../core/App';
10+
11+
12+
export class CustomConfig implements Configurable {
13+
14+
private log = new Log('config:CustomConfig');
15+
16+
public configure(app: App): void {
17+
this.log.debug('configuring', app.Express.get('port'));
18+
}
19+
}
20+

src/config/Database.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010

1111
import * as knex from 'knex';
1212
import * as bookshelf from 'bookshelf';
13-
import { Environment } from '../core/Environment';
1413

1514

1615
export const Knex = (): knex => knex({
17-
client: Environment.get<string>('DB_CLIENT'),
18-
connection: Environment.get<string>('DB_CONNECTION'),
16+
client: process.env.DB_CLIENT,
17+
connection: process.env.DB_CONNECTION,
1918
pool: {
20-
min: Environment.get<number>('DB_POOL_MIN'),
21-
max: Environment.get<number>('DB_POOL_MAX')
19+
min: parseInt(process.env.DB_POOL_MIN, 10),
20+
max: parseInt(process.env.DB_POOL_MAX, 10)
2221
}
2322
});
2423

src/config/IocConfig.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* IOC - CONTAINER
3+
* ----------------------------------------
4+
*
5+
* Bind every controller and service to the ioc container. All controllers
6+
* will then be bonded to the express structure with their defined routes.
7+
*/
8+
9+
// import { Container, decorate, injectable } from 'inversify';
10+
import { IoC } from '../core/IoC';
11+
// import { Types } from '../constants/Types';
12+
13+
// import * as request from 'request';
14+
15+
16+
export class IocConfig {
17+
public configure(ioc: IoC): void {
18+
// /**
19+
// * Here you can bind all your third-party libraries like
20+
// * request, lodash and so on. Those will be bound before
21+
// * everything else.
22+
// */
23+
// ioc.configureLib((container: Container) => {
24+
25+
// decorate(injectable(), request);
26+
27+
// container
28+
// .bind<any>(Types.Lib)
29+
// .toConstantValue(request)
30+
// .whenTargetNamed('request');
31+
32+
// return container;
33+
// });
34+
35+
// /**
36+
// * Bind custom classes here. This will be bound at the end
37+
// */
38+
// ioc.configure((container: Container) => {
39+
40+
// // Add your custom class here
41+
42+
// return container;
43+
// });
44+
}
45+
}
46+
47+

src/config/Logger.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)