-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (48 loc) · 1.94 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
const hbs = require('hbs');
const path = require('path');
const cors = require('cors');
const express = require('express');
const routes = require('./routes');
const logger = require('./config/logger.config');
const viewsPath = path.join(__dirname, './views');
const partialsPath = path.join(__dirname, './templates');
const constants = require('./config/constants.config');
const { json } = require('body-parser');
const publicDirectoryPath = path.join(__dirname, './public');
global.logger = logger;
global.constants = constants;
const app = express();
// for serving static files
app.use(express.static(publicDirectoryPath));
app.use(json());
// setup handlebars engine and views location
app.set('view engine', 'hbs');
app.set('views', viewsPath);
hbs.registerPartials(partialsPath);
// CORS controllers
app.use(cors());
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
res.header("Access-Control-Expose-Headers", "Authorization"); // for webgl support
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
next();
});
// routes
app.use(routes)
app.get('/', (req, res) => { res.render('auth', { title: 'Agora Web RTC' }); });
app.get('/chat', (req, res) => {
const cookie = req.headers.cookie;
let sUsername = cookie.split('sUsername=')[1].split(';')[0];
let sPassword = cookie.split('sPassword=')[1].split(';')[0];
if (sUsername && sPassword && sUsername === constants.USER && sPassword === constants.PASSWORD) {
res.render('index', { title: 'Agora Web RTC', appId: constants.AGORA_CONFIG.APP_ID });
} else {
res.render('auth', { title: 'Agora Web RTC' });
}
});
const live = app.listen(constants.PORT, () => {
const host = live.address().address;
const port = live.address().port;
logger.info(`Server is up on https://${host}:${port}`);
});