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,41 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var createError = require("http-errors");
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
var App = (function () {
function App() {
this.port = 3000;
this.AppMain = express();
this.setPlugins();
}
App.prototype.setPlugins = function () {
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();
};
App.prototype.setRouter = function () {
this.setError();
};
App.prototype.setError = function () {
this.AppMain.use(function (req, res, next) {
next(createError(404));
});
this.AppMain.use(function (err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
res.render('error');
});
};
return App;
}());
exports.App = App;

View File

@@ -0,0 +1,56 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var App_1 = require("./App");
var http = require("http");
var start = (function (_super) {
__extends(start, _super);
function start() {
var _this = _super.call(this) || this;
_this.server = http.createServer(_this.AppMain);
_this.run();
return _this;
}
start.prototype.run = function () {
this.server.listen(this.port);
this.server.on('error', this.onError);
this.server.on('listening', this.onListening);
};
start.prototype.onError = function (error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof this.port === 'string'
? 'Pipe ' + this.port
: 'Port ' + this.port;
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;
}
};
start.prototype.onListening = function () {
console.log('App is run: http://127.0.0.1:3000');
};
return start;
}(App_1.App));
new start();

View File

@@ -0,0 +1,25 @@
{
"name": "2019-08-13",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"w": "tsc -watch",
"dev": "node-dev dist/index.js",
"ts": "ts-node src/index.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/cookie-parser": "^1.4.1",
"@types/express": "^4.17.0",
"@types/http-errors": "^1.6.1",
"@types/morgan": "^1.7.36",
"@types/node": "^12.7.1",
"cookie-parser": "^1.4.4",
"express": "^4.17.1",
"http-errors": "^1.7.3",
"morgan": "^1.9.1"
}
}

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');
}
}

View File

@@ -0,0 +1,17 @@
{
"compilerOptions": {
"module": "commonjs",
"removeComments": true,
"esModuleInterop": true,
"preserveConstEnums": true,
"sourceMap": false,
"target": "es5",
"outDir": "dist/"
},
"exclude": [
"node_modules"
],
"include": [
"src/**/*"
],
}