first commit

This commit is contained in:
编码猿
2024-09-27 02:06:13 +08:00
commit 852d94fbb9
36760 changed files with 3274413 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
import { start } from "./run/start"
new start()

View File

@@ -0,0 +1,50 @@
import * as createError from "http-errors";
import * as express from "express";
import * as path from "path";
import * as cookieParser from "cookie-parser";
import * as logger from "morgan";
export class App {
protected AppMain: any;
public port:number = 3000;
constructor () {
this.AppMain = express();
this.setPlugins()
}
private setPlugins (): void {
this.AppMain.set('port', this.port)
this.AppMain.set('views', path.join(__dirname, '../views'));
this.AppMain.set('view engine', 'pug');
this.AppMain.use(logger('dev'));
this.AppMain.use(express.json());
this.AppMain.use(express.urlencoded({ extended: false }));
this.AppMain.use(cookieParser());
this.AppMain.use(express.static(path.join(__dirname, '../public')));
this.setRouter()
}
private setRouter (): void {
this.setError()
}
private setError (): void {
this.AppMain.use(function(req: any, res: any, next: (arg0: createError.HttpError) => void) {
next(createError(404));
});
// error handler
this.AppMain.use(function(err: { message: any; status: any; }, req: { app: { get: (arg0: string) => string; }; }, res: { locals: { message: any; error: any; }; status: (arg0: any) => void; render: (arg0: string) => void; }, next: any) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
}
}

View File

@@ -0,0 +1,45 @@
import { App } from "./App";
import * as http from "http";
export class start extends App {
private server:any;
constructor () {
super()
this.server = http.createServer(this.AppMain);
this.run()
}
run () {
this.server.listen(this.port);
this.server.on('error', this.onError);
this.server.on('listening', this.onListening);
}
onError (error:any) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof this.port === 'string'
? 'Pipe ' + this.port
: 'Port ' + this.port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
onListening() {
console.log('App is run: http://127.0.0.1:3000');
}
}