apply everything to ts; add temp layout
This commit is contained in:
89
src/App.ts
Normal file
89
src/App.ts
Normal file
@ -0,0 +1,89 @@
|
||||
import express from 'express';
|
||||
import path from "path";
|
||||
import morgan from "morgan";
|
||||
import cookieParser from "cookie-parser";
|
||||
import debug from "debug";
|
||||
import * as http from "http";
|
||||
|
||||
export class App {
|
||||
get app(): express.Application {
|
||||
return this._app;
|
||||
}
|
||||
|
||||
private readonly _app: express.Application;
|
||||
private readonly _server;
|
||||
|
||||
static port = 3000;
|
||||
|
||||
private log = debug("itender:app");
|
||||
|
||||
constructor() {
|
||||
this._app = express();
|
||||
this._server = http.createServer(this._app);
|
||||
|
||||
this._app.set('views', path.join(__dirname, '../views'));
|
||||
this._app.set('view engine', 'pug');
|
||||
|
||||
this._app.use(morgan('dev'));
|
||||
this._app.use(express.json());
|
||||
this._app.use(express.urlencoded({extended: false}));
|
||||
this._app.use(cookieParser());
|
||||
this._app.use(express.static(path.join(__dirname, "../public")));
|
||||
this._app.use('/web.js', express.static(path.join(__dirname, "../dist/web.bundle.js")));
|
||||
|
||||
|
||||
|
||||
this._app.use( (req, res, next) => {
|
||||
next();
|
||||
} )
|
||||
this._app.use((err, req, res, next) => {
|
||||
|
||||
res.locals.message = err.message;
|
||||
res.locals.error = err;
|
||||
|
||||
res.status(err.status || 500);
|
||||
res.render('error');
|
||||
this.log("Error " + err);
|
||||
});
|
||||
this.loadRoutes();
|
||||
}
|
||||
|
||||
public loadRoutes( ) : void
|
||||
{
|
||||
this._app.use( "/", require("./routes/index") );
|
||||
|
||||
}
|
||||
|
||||
public listen(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._server.on('error', (error) => {
|
||||
if (error.syscall != 'listen') {
|
||||
reject();
|
||||
return;
|
||||
}
|
||||
|
||||
let bind = 'Port ' + App.port;
|
||||
|
||||
// handle specific listen errors with friendly messages
|
||||
switch (error.code) {
|
||||
case 'EACCES':
|
||||
reject(bind + ' requires elevated privileges');
|
||||
break;
|
||||
case 'EADDRINUSE':
|
||||
reject(bind + ' is already in use');
|
||||
break;
|
||||
default:
|
||||
reject();
|
||||
}
|
||||
|
||||
});
|
||||
this._server.on('listening', () => {
|
||||
let addr = this._server.address();
|
||||
this.log("Listening on " + addr.port);
|
||||
resolve();
|
||||
})
|
||||
this._server.listen(App.port);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
77
src/WebsocketApp.ts
Normal file
77
src/WebsocketApp.ts
Normal file
@ -0,0 +1,77 @@
|
||||
import express from 'express';
|
||||
import morgan from "morgan";
|
||||
import debug from "debug";
|
||||
import expressWs from "express-ws";
|
||||
import http from "http";
|
||||
|
||||
export class WebsocketApp {
|
||||
get app(): express.Application {
|
||||
return this._app;
|
||||
}
|
||||
|
||||
private readonly _app: express.Application;
|
||||
private readonly _server;
|
||||
|
||||
static port = 3005;
|
||||
|
||||
private log = debug("itender:websocket-app");
|
||||
|
||||
constructor() {
|
||||
this._app = express();
|
||||
this._server = http.createServer(this._app);
|
||||
expressWs(this._app, this._server);
|
||||
|
||||
this._app.use(morgan('dev'));
|
||||
this._app.use(express.json());
|
||||
this._app.use(express.urlencoded({extended: false}));
|
||||
|
||||
this._app.set('trust proxy', 0);
|
||||
|
||||
this._app.use((err, req, res, next) => {
|
||||
res.status(err.status || 500);
|
||||
res.render('error');
|
||||
this.log("Error " + err);
|
||||
});
|
||||
|
||||
this._app.set("port", WebsocketApp.port);
|
||||
|
||||
}
|
||||
|
||||
public loadRoutes( ) : void
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public listen(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._server.on('error', (error) => {
|
||||
if (error.syscall != 'listen') {
|
||||
reject();
|
||||
return;
|
||||
}
|
||||
|
||||
let bind = 'Port ' + WebsocketApp.port;
|
||||
|
||||
// handle specific listen errors with friendly messages
|
||||
switch (error.code) {
|
||||
case 'EACCES':
|
||||
reject(bind + ' requires elevated privileges');
|
||||
break;
|
||||
case 'EADDRINUSE':
|
||||
reject(bind + ' is already in use');
|
||||
break;
|
||||
default:
|
||||
reject();
|
||||
}
|
||||
|
||||
});
|
||||
this._server.on('listening', () => {
|
||||
let addr = this._server.address();
|
||||
this.log("Listening on " + addr.port);
|
||||
resolve();
|
||||
})
|
||||
this._server.listen(WebsocketApp.port);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
24
src/main.ts
Normal file
24
src/main.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import {App} from "./App";
|
||||
import debug from "debug";
|
||||
import {WebsocketApp} from "./WebsocketApp";
|
||||
|
||||
const log = debug("itender:server");
|
||||
|
||||
const app = new App();
|
||||
const wsApp = new WebsocketApp();
|
||||
|
||||
|
||||
|
||||
(async( ) => {
|
||||
try {
|
||||
log("Starting...")
|
||||
await app.listen();
|
||||
await wsApp.listen();
|
||||
} catch( e )
|
||||
{
|
||||
console.error("---- ERROR ----")
|
||||
console.error(e);
|
||||
process.exit(-1);
|
||||
}
|
||||
})();
|
||||
|
10
src/routes/index.ts
Normal file
10
src/routes/index.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import express from "express";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
/* GET home page. */
|
||||
router.get('/', function (req, res, next) {
|
||||
res.render('index', {title: 'Express'});
|
||||
});
|
||||
|
||||
module.exports = router;
|
9
src/routes/websocket.js
Normal file
9
src/routes/websocket.js
Normal file
@ -0,0 +1,9 @@
|
||||
var express = require('express');
|
||||
var router = express.Router();
|
||||
|
||||
/* GET users listing. */
|
||||
router.get('/', function(req, res, next) {
|
||||
res.send('respond with a resource');
|
||||
});
|
||||
|
||||
module.exports = router;
|
@ -0,0 +1,33 @@
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
console.log("DOM Loaded");
|
||||
|
||||
const main = document.getElementById("main");
|
||||
const time = document.getElementById("time");
|
||||
if(!main||!time)
|
||||
return;
|
||||
let currentDate = new Date();
|
||||
|
||||
|
||||
setInterval( () => {
|
||||
currentDate = new Date();
|
||||
time.innerText = "" + ( currentDate.getHours() < 10 ? "0" + currentDate.getHours() : currentDate.getHours() ) + ":" + ( currentDate.getMinutes() < 10 ? "0" + currentDate.getMinutes() : currentDate.getMinutes() );
|
||||
}, 1000 );
|
||||
|
||||
let maxI = 20;
|
||||
|
||||
main.style.gridTemplateRows = `repeat(${Math.round(maxI/3)}, calc(90%/2))`;
|
||||
|
||||
|
||||
for( let i = 0; i<maxI; i++ )
|
||||
{
|
||||
let testDrink = document.createElement("div");
|
||||
testDrink.classList.add("drink");
|
||||
|
||||
main.append(testDrink);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
@ -1,22 +0,0 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"target": "ES6",
|
||||
"module": "CommonJS",
|
||||
"lib": [
|
||||
"es5",
|
||||
"es6",
|
||||
"dom",
|
||||
"dom.iterable"
|
||||
],
|
||||
"allowJs": true,
|
||||
"sourceMap": true,
|
||||
"downlevelIteration": true,
|
||||
"baseUrl": "src/web/",
|
||||
"paths": {
|
||||
"*": [
|
||||
"src/web/*",
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user