From d961b7a75bf3f0d25339214e1a9535533a25fd36 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Tue, 5 Mar 2019 13:57:17 +0100 Subject: Refactored code structure --- app.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 app.js (limited to 'app.js') diff --git a/app.js b/app.js new file mode 100644 index 0000000..66f98a4 --- /dev/null +++ b/app.js @@ -0,0 +1,56 @@ +/* + * app.js + * Copyright (c) 2019, Texx + * License: MIT + * See https://github.com/texxme/Texx/blob/master/LICENSE + */ + +import { ExpressPeerServer } from 'peer'; +import express, { Router } from 'express'; +import path from 'path'; +import logger from 'morgan'; +import bodyParser from 'body-parser'; + +const app = express(); +app.disable('x-powered-by'); + +const routes = Router(); +const server = app.listen(8080, '0.0.0.0'); +const peerServer = ExpressPeerServer(server, { debug: true }); + +peerServer.on('connection', id => console.log(`New connection: ${id}`)); + +routes.get('/', (req, res) => { + res.render('index'); +}); +app.use('/api', peerServer); + +// View engine setup +app.set('views', path.join(__dirname, 'views')); +app.set('view engine', 'pug'); + +app.use(logger('dev', { + skip: () => app.get('env') === 'test', +})); +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: false })); +app.use(express.static(path.join(__dirname, 'dist'))); + +// Routes +app.use('/', routes); + +// Catch 404 and forward to error handler +app.use((req, res, next) => { + const err = new Error('Not Found'); + err.status = 404; + next(err); +}); + +// Error handler +app.use((err, req, res, next) => { // eslint-disable-line no-unused-vars + res + .status(err.status || 500) + .render('error', { + message: err.message, + }); +}); -- cgit v1.2.3