This repository has been archived by the owner on Aug 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
97 lines (88 loc) · 3.13 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
let port = normalizePort(process.argv[2] || process.env.PORT || '3000');
if (process.argv[3] === "development") {
process.env.NODE_ENV = "development";
} else {
process.env.NODE_ENV = "production";
}
const portRange = port + 10;
const express = require('express');
const createError = require("http-errors");
const app = express();
const http = require('http');
const server = http.createServer(app);
const io = require('socket.io').listen(server);
const jpegSocket = require('./jpeg')(app, io);
const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
console.log(process.env.NODE_ENV);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.set('io', io);
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use("/javascripts/jquery.min.js", express.static(path.join(__dirname, 'node_modules', 'jquery', 'dist', 'jquery.min.js')));
app.use("/javascripts/socket.io.js", express.static(path.join(__dirname, 'node_modules', 'socket.io-client', 'dist', 'socket.io.js')));
app.use("/javascripts/material.min.js", express.static(path.join(__dirname, 'node_modules', 'material-design-lite', 'material.min.js')));
app.use("/stylesheets/material.grey-indigo.min.css", express.static(path.join(__dirname, 'node_modules', 'material-design-lite', 'dist', 'material.grey-indigo.min.css')));
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.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');
});
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
function normalizePort(val) {
const port = parseInt(val, 10);
if (isNaN(port)) {
return val;
}
if (port >= 0) {
return port;
}
return !1;
}
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
return process.exit(1);
case 'EADDRINUSE':
console.error(`${bind} is already in use.`);
if (typeof port === 'string' || port >= portRange) {
return process.exit(1);
}
console.log(`Incrementing to port ${++port} and trying again.`);
server.listen(port);
break;
default:
throw error;
}
}
function onListening() {
const addr = server.address();
const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
console.log(`Listening on ${bind}.`);
}
module.exports = app;