-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
76 lines (57 loc) · 2.24 KB
/
app.js
File metadata and controls
76 lines (57 loc) · 2.24 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
73
74
75
76
const express = require(`express`);
const mongoose = require(`mongoose`);
const http = require('http');
const use = require(`./routes/user`);
const {doc,title} = require(`./routes/document`);
const pages = require(`./routes/pages`);
const config = require('config');
var WebSocket = require('ws');
var WebSocketJSONStream = require('websocket-json-stream');
var path = require('path');
var ShareDB = require('sharedb');
const auth = require(`./middleware/authTok`);
const cookieParser = require('cookie-parser')
const cookieSecret ='thelandbeforetime'; //Major security issue- change later
const { User, validateUser} = require(`./models/userModel`);
const app = express();
const server = http.createServer(app);
const{createDoc,startSocket,startSocket2} = require('./server');
const db = require('sharedb-mongo')('mongodb://localhost:27017/Conc');
const backend = new ShareDB({db});
const port = process.env.PORT || 8080;
// connecting to Mongo Database
mongoose.connect('mongodb://localhost/Conc',{ useNewUrlParser: true })
.then(()=>console.log(`Connected to mongoDB`))
.catch(err =>console.error(`Could not connect to MongoDB`));
app.use(express.json());//converts incoming information into json
app.use(express.urlencoded({extended:true})); //converts into url form encoded
app.use(express.static(path.join(__dirname, 'public')));//display static files directly to the site
app.use(express.static('node_modules/quill/dist'));
app.set('view engine','ejs');
app.use(cookieParser(cookieSecret));
// startSocket server for client connections
var wss = new WebSocket.Server({server: server});
wss.on('connection', function(ws, req) {
ws.on('close', function() {
console.log('Client disconnected');
});
var stream = new WebSocketJSONStream(ws);
console.log('User has connected');
backend.listen(stream);
});
//Load specific routes
app.get('/', async (req,res) => {
res.render('index');
});
app.get('/:id', async (req,res) => {
res.render('textdoc');
});
//Loading in more specific functional routes
app.use(`/user`,use);
app.use(`/session`,doc);
app.use(`/pages`,pages);
server.listen(port, () => {
console.log(`Listening on port ${port}....`);
});
module.exports.websiteapp = app;
module.exports.websiteserver = server;