Removing uneccessary log
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is passing
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	continuous-integration/drone/push Build is passing
				
			This commit is contained in:
		
							
								
								
									
										108
									
								
								src/web.ts
									
									
									
									
									
								
							
							
						
						
									
										108
									
								
								src/web.ts
									
									
									
									
									
								
							| @ -1,44 +1,47 @@ | |||||||
| import { WebConfig } from "./config"; | import { WebConfig } from "./config"; | ||||||
| import * as express from "express" | import * as express from "express"; | ||||||
| import { Express } from "express" | import { Express } from "express"; | ||||||
|  |  | ||||||
| import Logging from "@hibas123/nodelogging" | import Logging from "@hibas123/nodelogging"; | ||||||
|  |  | ||||||
| import * as bodyparser from "body-parser"; | import * as bodyparser from "body-parser"; | ||||||
| import * as cookieparser from "cookie-parser" | import * as cookieparser from "cookie-parser"; | ||||||
|  |  | ||||||
| import * as i18n from "i18n" | import * as i18n from "i18n"; | ||||||
| import * as compression from "compression"; | import * as compression from "compression"; | ||||||
| import ApiRouter from "./api"; | import ApiRouter from "./api"; | ||||||
| import ViewRouter from "./views/views"; | import ViewRouter from "./views/views"; | ||||||
| import RequestError, { HttpStatusCode } from "./helper/request_error"; | import RequestError, { HttpStatusCode } from "./helper/request_error"; | ||||||
|  |  | ||||||
| export default class Web { | export default class Web { | ||||||
|    server: Express |    server: Express; | ||||||
|    private port: number |    private port: number; | ||||||
|  |  | ||||||
|    constructor(config: WebConfig) { |    constructor(config: WebConfig) { | ||||||
|       this.server = express() |       this.server = express(); | ||||||
|       this.port = Number(config.port); |       this.port = Number(config.port); | ||||||
|       this.registerMiddleware() |       this.registerMiddleware(); | ||||||
|       this.registerEndpoints() |       this.registerEndpoints(); | ||||||
|       this.registerErrorHandler() |       this.registerErrorHandler(); | ||||||
|    } |    } | ||||||
|  |  | ||||||
|    listen() { |    listen() { | ||||||
|       this.server.listen(this.port, () => { |       this.server.listen(this.port, () => { | ||||||
|          Logging.log(`Server listening on port ${this.port}`) |          Logging.log(`Server listening on port ${this.port}`); | ||||||
|       }) |       }); | ||||||
|    } |    } | ||||||
|  |  | ||||||
|    private registerMiddleware() { |    private registerMiddleware() { | ||||||
|       this.server.use(cookieparser()) |       this.server.use(cookieparser()); | ||||||
|       this.server.use(bodyparser.json(), bodyparser.urlencoded({ extended: true })) |       this.server.use( | ||||||
|       this.server.use(i18n.init) |          bodyparser.json(), | ||||||
|  |          bodyparser.urlencoded({ extended: true }) | ||||||
|  |       ); | ||||||
|  |       this.server.use(i18n.init); | ||||||
|  |  | ||||||
|       //Logging Middleware |       //Logging Middleware | ||||||
|       this.server.use((req, res, next) => { |       this.server.use((req, res, next) => { | ||||||
|          let start = process.hrtime() |          let start = process.hrtime(); | ||||||
|          let finished = false; |          let finished = false; | ||||||
|          let to = false; |          let to = false; | ||||||
|          let listener = () => { |          let listener = () => { | ||||||
| @ -47,55 +50,72 @@ export default class Web { | |||||||
|             let td = process.hrtime(start); |             let td = process.hrtime(start); | ||||||
|             let time = !to ? (td[0] * 1e3 + td[1] / 1e6).toFixed(2) : "--.--"; |             let time = !to ? (td[0] * 1e3 + td[1] / 1e6).toFixed(2) : "--.--"; | ||||||
|             let resColor = ""; |             let resColor = ""; | ||||||
|             if (res.statusCode >= 200 && res.statusCode < 300) resColor = "\x1b[32m" //Green |             if (res.statusCode >= 200 && res.statusCode < 300) | ||||||
|             else if (res.statusCode === 304 || res.statusCode === 302) resColor = "\x1b[33m" |                resColor = "\x1b[32m"; | ||||||
|             else if (res.statusCode >= 400 && res.statusCode < 500) resColor = "\x1b[36m" //Cyan |             //Green | ||||||
|             else if (res.statusCode >= 500 && res.statusCode < 600) resColor = "\x1b[31m" //Red |             else if (res.statusCode === 304 || res.statusCode === 302) | ||||||
|  |                resColor = "\x1b[33m"; | ||||||
|  |             else if (res.statusCode >= 400 && res.statusCode < 500) | ||||||
|  |                resColor = "\x1b[36m"; | ||||||
|  |             //Cyan | ||||||
|  |             else if (res.statusCode >= 500 && res.statusCode < 600) | ||||||
|  |                resColor = "\x1b[31m"; //Red | ||||||
|             let m = req.method; |             let m = req.method; | ||||||
|             while (m.length < 4) m += " "; |             while (m.length < 4) m += " "; | ||||||
|             Logging.log(`${m} ${req.originalUrl} ${req.language} ${resColor}${res.statusCode}\x1b[0m - ${time}ms`) |             Logging.log( | ||||||
|             res.removeListener("finish", listener) |                `${m} ${req.originalUrl} ${req.language} ${resColor}${res.statusCode}\x1b[0m - ${time}ms` | ||||||
|          } |             ); | ||||||
|          res.on("finish", listener) |             res.removeListener("finish", listener); | ||||||
|  |          }; | ||||||
|  |          res.on("finish", listener); | ||||||
|          setTimeout(() => { |          setTimeout(() => { | ||||||
|             to = true; |             to = true; | ||||||
|             listener(); |             listener(); | ||||||
|          }, 2000) |          }, 2000); | ||||||
|          next() |          next(); | ||||||
|       }) |       }); | ||||||
|  |  | ||||||
|       this.server.use(compression({ |       this.server.use( | ||||||
|          filter: (req, res) => { |          compression({ | ||||||
|             if (req.headers['x-no-compression']) { |             filter: (req, res) => { | ||||||
|                return false |                if (req.headers["x-no-compression"]) { | ||||||
|             } |                   return false; | ||||||
|             return compression.filter(req, res) |                } | ||||||
|          } |                return compression.filter(req, res); | ||||||
|       })); |             }, | ||||||
|  |          }) | ||||||
|  |       ); | ||||||
|    } |    } | ||||||
|  |  | ||||||
|    private registerEndpoints() { |    private registerEndpoints() { | ||||||
|       this.server.use("/api", ApiRouter); |       this.server.use("/api", ApiRouter); | ||||||
|       this.server.use("/", ViewRouter) |       this.server.use("/", ViewRouter); | ||||||
|    } |    } | ||||||
|  |  | ||||||
|    private registerErrorHandler() { |    private registerErrorHandler() { | ||||||
|       this.server.use((error, req: express.Request, res, next) => { |       this.server.use((error, req: express.Request, res, next) => { | ||||||
|          if (!(error instanceof RequestError)) { |          if (!(error instanceof RequestError)) { | ||||||
|             error = new RequestError(error.message, error.status || HttpStatusCode.INTERNAL_SERVER_ERROR, error.nolog || false); |             error = new RequestError( | ||||||
|  |                error.message, | ||||||
|  |                error.status || HttpStatusCode.INTERNAL_SERVER_ERROR, | ||||||
|  |                error.nolog || false | ||||||
|  |             ); | ||||||
|          } |          } | ||||||
|  |  | ||||||
|          if (error.status === 500 && !(<any>error).nolog) { |          if (error.status === 500 && !(<any>error).nolog) { | ||||||
|             Logging.error(error); |             Logging.error(error); | ||||||
|          } else { |          } else { | ||||||
|             Logging.log("Responded with Error:", typeof error.message === "string" ? error.message.split("\n", 1)[0] : error.message); |             Logging.log("Responded with Error", error.status); | ||||||
|          } |          } | ||||||
|  |  | ||||||
|          if (req.accepts(["json"])) { |          if (req.accepts(["json"])) { | ||||||
|             res.json_status = error.status || 500; |             res.json_status = error.status || 500; | ||||||
|             res.json({ error: error.message, status: error.status || 500, additional: error.additional }) |             res.json({ | ||||||
|          } else |                error: error.message, | ||||||
|             res.status(error.status || 500).send(error.message) |                status: error.status || 500, | ||||||
|       }) |                additional: error.additional, | ||||||
|  |             }); | ||||||
|  |          } else res.status(error.status || 500).send(error.message); | ||||||
|  |       }); | ||||||
|    } |    } | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Fabian Stamm
					Fabian Stamm