-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
62 lines (49 loc) · 1.73 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
import express from 'express'
import mustacheExpress from 'mustache-express';
import { fileURLToPath } from 'url';
import path, {dirname} from 'path'
import ip from 'ip'
import fs from 'fs'
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const configPath = path.join(process.cwd(), 'config.json')
//check if ConfigPath exists and if not create it with default settings
export let config = {
port: 3000,
obsHost: "localhost:4000",
obsPassword: null,
obsUsername: null,
screenShotTime: 5000
}
if(!fs.existsSync(configPath)){
fs.writeFileSync(configPath, JSON.stringify(config, null, 4))
}
else {
// read config file and spread with default settings
const configFile = fs.readFileSync(configPath)
config = {...config, ...JSON.parse(configFile)}
}
const app = express()
// function to get the local ip address
const localIp = ip.address()
app.engine('mst', mustacheExpress())
app.set('view engine', 'mst')
app.set('views', path.join(__dirname, 'views'))
app.use(express.static(path.join(__dirname, 'public')))
//CORS Middleware
app.use(function (req, res, next) {
//Enabling CORS
//Allow localhost and local ip address
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
next();
});
app.route('/').get((req,res) => {
// console.log('Here')
res.render('index', {'ip': localIp, 'port': config.port, 'screenShotTime': config.screenShotTime})
})
app.route('/obs').get((req,res) => {
res.render('obs', {'ip': localIp, 'port': config.port})
})
export default app;