├── src ├── controllers │ ├── index.ts │ └── ExampleController.ts ├── start.ts └── ExampleServer.ts ├── README.md ├── .gitignore ├── util └── nodemon.json ├── tslint.json ├── tsconfig.json ├── LICENSE └── package.json /src/controllers/index.ts: -------------------------------------------------------------------------------- 1 | export * from './ExampleController'; 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ExpressTypeScript 2 | Develop an ExpressJS webserver using TypeScript 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # ingore file 2 | 3 | node_modules 4 | package-lock.json 5 | 6 | .idea 7 | build 8 | -------------------------------------------------------------------------------- /util/nodemon.json: -------------------------------------------------------------------------------- 1 | { 2 | "watch": ["src"], 3 | "ext": "ts", 4 | "ignore": ["src/public"], 5 | "exec": "ts-node src/start.ts" 6 | } 7 | -------------------------------------------------------------------------------- /src/start.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Start the Express Web-Server 3 | * 4 | * created by Sean Maxwell Apr 14, 2019 5 | */ 6 | 7 | import ExampleServer from './ExampleServer'; 8 | 9 | const exampleServer = new ExampleServer(); 10 | exampleServer.start(3000); 11 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tslint:recommended", 3 | "rules": { 4 | "lines-between-class-members": [true, 2], 5 | "max-line-length": { 6 | "options": [100] 7 | }, 8 | "member-ordering": false, 9 | "no-consecutive-blank-lines": false, 10 | "object-literal-sort-keys": false, 11 | "ordered-imports": false, 12 | "quotemark": [true, "single"], 13 | "variable-name": [true, "allow-leading-underscore"] 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "strict": true, 5 | "baseUrl": "./", 6 | "outDir": "build", 7 | "removeComments": true, 8 | "experimentalDecorators": true, 9 | "target": "es6", 10 | "emitDecoratorMetadata": true, 11 | "moduleResolution": "node", 12 | "importHelpers": true, 13 | "types": [ 14 | "node" 15 | ], 16 | "typeRoots": [ 17 | "node_modules/@types" 18 | ] 19 | }, 20 | "include": [ 21 | "./src/**/*.ts" 22 | ], 23 | "exclude": [ 24 | "./src/public/" 25 | ] 26 | } 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Sean Maxwell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "expresstypescript", 3 | "version": "1.0.0", 4 | "description": "Develop and ExpressJS webserver using TypeScript", 5 | "main": "build/start.js", 6 | "scripts": { 7 | "test": "none", 8 | "start-dev": "nodemon --config \"./util/nodemon.json\"/", 9 | "build": "rm -rf ./build && tsc", 10 | "start": "node build/start.js" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/seanpmaxwell/ExpressTypeScript.git" 15 | }, 16 | "keywords": [ 17 | "Express", 18 | "TypeScript", 19 | "ts", 20 | "ex", 21 | "expressjs", 22 | "overnight", 23 | "overnightjs", 24 | "node", 25 | "web", 26 | "webserver", 27 | "server" 28 | ], 29 | "author": "sean maxwell", 30 | "license": "MIT", 31 | "bugs": { 32 | "url": "https://github.com/seanpmaxwell/ExpressTypeScript/issues" 33 | }, 34 | "homepage": "https://github.com/seanpmaxwell/ExpressTypeScript#readme", 35 | "dependencies": { 36 | "@overnightjs/core": "^1.6.4", 37 | "@overnightjs/logger": "^1.1.4", 38 | "body-parser": "^1.18.3", 39 | "express": "^4.16.4" 40 | }, 41 | "devDependencies": { 42 | "@types/express": "^4.16.1", 43 | "@types/node": "^11.13.4", 44 | "nodemon": "^1.18.11", 45 | "ts-node": "^8.1.0", 46 | "tslint": "^5.15.0", 47 | "typescript": "^3.4.3" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/ExampleServer.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Server file for ExpressJS 3 | * 4 | * created by Sean Maxwell April 14, 2019 5 | */ 6 | 7 | import * as bodyParser from 'body-parser'; 8 | import * as controllers from './controllers'; 9 | 10 | import { Server } from '@overnightjs/core'; 11 | import { Logger } from '@overnightjs/logger'; 12 | 13 | 14 | class ExampleServer extends Server { 15 | 16 | private readonly SERVER_STARTED = 'Example server started on port: '; 17 | 18 | 19 | constructor() { 20 | super(true); 21 | this.app.use(bodyParser.json()); 22 | this.app.use(bodyParser.urlencoded({extended: true})); 23 | this.setupControllers(); 24 | } 25 | 26 | 27 | private setupControllers(): void { 28 | const ctlrInstances = []; 29 | for (const name in controllers) { 30 | if (controllers.hasOwnProperty(name)) { 31 | const controller = (controllers as any)[name]; 32 | ctlrInstances.push(new controller()); 33 | } 34 | } 35 | super.addControllers(ctlrInstances); 36 | } 37 | 38 | 39 | public start(port: number): void { 40 | this.app.get('*', (req, res) => { 41 | res.send(this.SERVER_STARTED + port); 42 | }); 43 | this.app.listen(port, () => { 44 | Logger.Imp(this.SERVER_STARTED + port); 45 | }); 46 | } 47 | } 48 | 49 | export default ExampleServer; 50 | -------------------------------------------------------------------------------- /src/controllers/ExampleController.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Example controller 3 | * 4 | * created by Sean Maxwell Apr 14, 2019 5 | */ 6 | 7 | import { Request, Response } from 'express'; 8 | import { Controller, Middleware, Get, Put, Post, Delete } from '@overnightjs/core'; 9 | import { Logger } from '@overnightjs/logger'; 10 | 11 | 12 | @Controller('api') 13 | export class ExampleController { 14 | 15 | 16 | @Get(':msg') 17 | private getMessage(req: Request, res: Response) { 18 | Logger.Info(req.params.msg); 19 | res.status(200).json({ 20 | message: req.params.msg, 21 | }); 22 | } 23 | 24 | 25 | @Put(':msg') 26 | private putMessage(req: Request, res: Response) { 27 | Logger.Info(req.params.msg); 28 | return res.status(400).json({ 29 | error: req.params.msg, 30 | }); 31 | } 32 | 33 | 34 | @Post(':msg') 35 | private postMessage(req: Request, res: Response) { 36 | Logger.Info(req.params.msg); 37 | return res.status(400).json({ 38 | error: req.params.msg, 39 | }); 40 | } 41 | 42 | 43 | @Delete(':msg') 44 | private delMessage(req: Request, res: Response) { 45 | try { 46 | throw new Error(req.params.msg); 47 | } catch (err) { 48 | Logger.Err(err, true); 49 | return res.status(400).json({ 50 | error: req.params.msg, 51 | }); 52 | } 53 | } 54 | } 55 | --------------------------------------------------------------------------------