-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
132 lines (100 loc) · 3.53 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var passport= require("passport");
var LocalStrategy = require("passport-local");
var User = require("./models/user");
mongoose.connect("mongodb://localhost:27017/ano_chat",{ useNewUrlParser: true });
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname + '/public'));
//PASSPORT CONFIGURATION
app.use(require("express-session")({
secret: "Can't tell you it's super secret!",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.get("/", function(req, res){
res.render("landing.ejs");
});
app.get("/chat",function(req,res){
res.render("chat.ejs");
})
// ==============
// Auth Routes
// ==============
app.post("/chat", function(req, res){
var newUser = new User({username: req.body.username});
User.register(newUser, req.body.password, function(err, user){
if(err){
console.log(err);
return res.render("landing.ejs");
}
passport.authenticate("local")(req, res, function(){
res.render("chat.ejs");
});
});
});
app.listen(3000 || process.env.PORT, process.env.IP, function(){
console.log("The server has started!!!");
});
*/
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const http = require('http');
const container = require('./container');
const cookieParser = require('cookie-parser');
const validator = require('express-validator');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const mongoose = require('mongoose');
const flash = require('connect-flash');
const passport = require('passport');
const socketIO = require('socket.io');
const {Global} = require('./helpers/Global');
const {Users}= require('./helpers/UsersClass');
container.resolve(function (users, _) {
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/anochat', {useNewUrlParser: true});
const app = SetupExpress();
function SetupExpress() {
const app = express();
const server = http.createServer(app);
const io = socketIO(server);
server.listen(process.env.PORT || 3000, function () {
console.log("Server started on port 3000!!!");
});
ConfigureExpress(app);
require('./socket/globalroom')(io,Global,_);
require('./socket/groupchat')(io,Users,_);
//Setup Router
const router = require('express-promise-router')();
users.SetRouting(router);
app.use(router);
}
function ConfigureExpress(app) {
require('./passport/passport-local');
app.use(express.static('public'));
app.use(cookieParser());
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(validator());
app.use(session({
secret: "Can't tell you it's super secret!",
resave: true,
saveUninitialized: true,
store: new MongoStore({ mongooseConnection: mongoose.connection })
}))
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.locals._ = _;
}
});