forked from ThomasMcDonald/MEAN-Stack-Chat-Room
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (51 loc) · 2.41 KB
/
server.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
var express = require('express')
var bodyParser = require('body-parser')
var cors = require('cors')
var chalk = require('chalk');
var mongoose = require('mongoose');
var logger = require('winston');
var jwt = require("jsonwebtoken");
var bcrypt = require('bcrypt');
var multer = require('multer'); // for the file upload
var app = express()
var http = require('http').Server(app)
var bodyPaser = bodyParser.json()
const fs = require('fs');
var port = process.env.PORT || 8080;
var activeData = require(__dirname + '/server/Utils/database.js');
// Database connection: (Change this to what your database URL is!)
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/chatFactory',{ useNewUrlParser: true });
var db = mongoose.connection;
require(__dirname + '/server/Utils/database.js')(chalk, db);
// Models - database Schemas
var models = {
mongoose: mongoose,
user: require(__dirname + '/server/models/users')(mongoose,bcrypt),
group: require(__dirname + '/server/models/groups')(mongoose,bcrypt),
channel: require(__dirname + '/server/models/channels')(mongoose,bcrypt),
message: require(__dirname + '/server/models/messages')(mongoose,bcrypt)
};
//Controllers - database functions
var controller = {
user: require(__dirname + '/server/controllers/users')(models, logger,jwt,bcrypt,multer),
group: require(__dirname + '/server/controllers/groups')(models, logger,jwt,bcrypt),
channel: require(__dirname + '/server/controllers/channels')(models, logger,jwt,bcrypt),
message: require(__dirname + '/server/controllers/messages')(models, logger,jwt,bcrypt)
};
app.use(express.static(__dirname + '/dist/Chat-Factory'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyPaser);
var server = app.listen(port, function () {
var host = server.address().address
var port = server.address().port
console.log("Listening on %s", port)
})
var io = require('socket.io').listen(server);
require(__dirname + '/server/Utils/sockets')(models, controller, app, io)
require(__dirname + '/server/Utils/routes')(models, controller, app, express, io)
setupData();
// Adding a default user, this will run each time the server starts up, but wont add duplicates
async function setupData(){
return await controller.user.createUser({_email:"[email protected]",_username:"super",_password:"Super",_role: "Super",_profileImage:"/server/uploads",_inChannel:[],_inGroup:[]})
}
module.exports = server;