aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--.env.example1
-rw-r--r--README.md1
-rw-r--r--public/scripts/chat.js3
-rw-r--r--src/app.js40
-rw-r--r--src/index.js48
-rw-r--r--src/routes.js24
-rw-r--r--test/routes.test.js28
7 files changed, 47 insertions, 98 deletions
diff --git a/.env.example b/.env.example
deleted file mode 100644
index 25241b7..0000000
--- a/.env.example
+++ /dev/null
@@ -1 +0,0 @@
-PORT=8080
diff --git a/README.md b/README.md
index 91e0335..e785539 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,6 @@
## Dev instructions:
* clone repo
-* copy `.env.example` to `.env`
* `npm install`
* `npm run css`
* run these in different windows/tabs every time you want to start the server
diff --git a/public/scripts/chat.js b/public/scripts/chat.js
index 711912b..5f437d9 100644
--- a/public/scripts/chat.js
+++ b/public/scripts/chat.js
@@ -6,6 +6,7 @@ const nolookalikes = require('nanoid-dictionary/nolookalikes');
let connectedPeers = []; // TODO: Save new peers in array
let connectedPeer;
const peerId = generate(nolookalikes, 16);
+const host = '127.0.0.1';
// setup encryption
(async () => {
@@ -20,7 +21,7 @@ const peerId = generate(nolookalikes, 16);
})();
function chat() {
- const peer = new Peer(peerId, {host: '127.0.0.1', port: 4242, path: '/', debug: 0});
+ const peer = new Peer(peerId, {host: host, port: 8080, path: '/api', debug: 0});
// Peer events
peer.on('open', id => console.log('[LOG] Your ID is', id));
diff --git a/src/app.js b/src/app.js
deleted file mode 100644
index 0b0b3cd..0000000
--- a/src/app.js
+++ /dev/null
@@ -1,40 +0,0 @@
-import express from 'express';
-import path from 'path';
-import logger from 'morgan';
-import bodyParser from 'body-parser';
-import routes from './routes';
-
-const app = express();
-app.disable('x-powered-by');
-
-// 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
- });
-});
-
-export default app;
diff --git a/src/index.js b/src/index.js
index fbfb85e..c664485 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,4 +1,46 @@
-import app from './app';
+import {ExpressPeerServer} from "peer";
+import express from 'express';
+import path from 'path';
+import logger from 'morgan';
+import bodyParser from 'body-parser';
+import routes from './routes';
-const {PORT = 8080} = process.env;
-app.listen(PORT, () => console.log(`Listening on port ${PORT}`)); // eslint-disable-line no-console
+const app = express();
+app.disable('x-powered-by');
+
+const server = app.listen(8080, "0.0.0.0");
+const peerServer = ExpressPeerServer(server, {debug: true});
+
+peerServer.on('connection', id => console.log('New connection: ' + id));
+
+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
+ });
+});
diff --git a/src/routes.js b/src/routes.js
index c6590da..ef00e68 100644
--- a/src/routes.js
+++ b/src/routes.js
@@ -9,28 +9,4 @@ routes.get('/', (req, res) => {
res.render('index');
});
-/**
- * GET /list
- *
- * This is a sample route demonstrating
- * a simple approach to error handling and testing
- * the global error handler. You most certainly want to
- * create different/better error handlers depending on
- * your use case.
- */
-routes.get('/list', (req, res, next) => {
- const {title} = req.query;
-
- if (title == null || title === '') {
- // You probably want to set the response HTTP status to 400 Bad Request
- // or 422 Unprocessable Entity instead of the default 500 of
- // the global error handler (e.g check out https://github.com/kbariotis/throw.js).
- // This is just for demo purposes.
- next(new Error('The "title" parameter is required'));
- return;
- }
-
- res.render('index', {title});
-});
-
export default routes;
diff --git a/test/routes.test.js b/test/routes.test.js
deleted file mode 100644
index 025c17c..0000000
--- a/test/routes.test.js
+++ /dev/null
@@ -1,28 +0,0 @@
-import request from 'supertest';
-import app from '../src/app.js';
-
-describe('GET /', () => {
- it('should render properly', async () => {
- await request(app).get('/').expect(200);
- });
-});
-
-describe('GET /list', () => {
- it('should render properly with valid parameters', async () => {
- await request(app)
- .get('/list')
- .query({title: 'List title'})
- .expect(200);
- });
-
- it('should error without a valid parameter', async () => {
- await request(app).get('/list').expect(500);
- });
-});
-
-describe('GET /404', () => {
- it('should return 404 for non-existent URLs', async () => {
- await request(app).get('/404').expect(404);
- await request(app).get('/notfound').expect(404);
- });
-});