-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
72 lines (60 loc) · 1.94 KB
/
Copy pathapp.js
File metadata and controls
72 lines (60 loc) · 1.94 KB
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
//Library Dependenciies
const express = require("express");
const { Server } = require("socket.io");
const http = require('http')
const bodyParser = require('body-parser')
const fs = require("fs");
const { json } = require("body-parser");
require("dotenv").config();
//Connection Tracker
var connections = 0;
//Settings
const port = 80;
/*<--- set dynamic rendering --->*/
//Server initialization (with socketio)
const app = express();
const server = http.createServer(app);
const io = new Server(server);
//uses ejs view engine for dynamic webpaging
app.set('view engine','ejs');
// Renders html pages
app.engine('html', require('ejs').renderFile);
//Host static files in "/public"
app.use(express.static('public', {extensions: ["html"]}))
app.use(bodyParser.urlencoded({extended: true}))
//set views
app.set('views', __dirname + '/public/html')
//handle hostname change post
app.post('/sethost', (req,res)=> {
if (req.body.password == process.env.HOSTNAMEPASS) {
var dat = JSON.parse(fs.readFileSync(__dirname +"/public/json/hostname.json"))
dat.hostname = req.body.ip
dat.port = req.body.port
fs.writeFileSync(__dirname +"/public/json/hostname.json",JSON.stringify(dat))
res.end(`host set to ${req.body.ip}:${req.body.port}`)
}else {
res.end("password invalid")
}
})
//Route "/" to home.html dynamically
app.get('/', (req, res)=>{
res.render("home.html")
})
//Socket.io routing
io.on('connection', (connection)=> {
var address = connection.handshake.address;
connections += 1
connection.on('chat-message-receive', msg =>{
msg.name = address.replace("::ffff:", "")
io.emit('chat-message-receive', msg )
})
io.emit('connectionChange', connections)
connection.on('disconnect', ()=>{
connections -= 1
io.emit('connectionChange', connections)
})
})
//Start server listening
server.listen(port, ()=>{
console.log('Server listening on port:', port)
})