-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
100 lines (81 loc) · 2.49 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
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
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const cors = require('cors')
require('dotenv').config();
// database connection
let connectDb = require('./db/db')
connectDb().then(async () => {
console.log("Database connected")
}).catch(error=>{
console.log(error)
console.log("Error connecting database !")
})
// var admin = require("firebase-admin");
// var serviceAccount = require("./storio-aaa61-firebase-adminsdk-3g0hh-f9c8a5402d.json");
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var productRouter = require('./routes/products')
var paymentRouter = require('./routes/payment')
var userAuth = require('./middlewares/auth/userAuth')
var app = express();
// socket.io setup
var io = require('socket.io')({}, {
cors: {
origin: "*",
credentials: true
}
});
app.io = io;
// firebase admin setup
// admin.initializeApp({
// credential: admin.credential.cert(serviceAccount),
// databaseURL: "https://storio-aaa61-default-rtdb.asia-southeast1.firebasedatabase.app"
// });
// var db = admin.database();
// route for virtual cart
var cartRouter = require('./routes/virtualCart')(io);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(cors())
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/virtual-cart', () => {
io.on('connection', socket => {
let { id } = socket;
console.log('new connection iddddd:', id);
socket.on('disconnect', socket => {
console.log('disconnect id:', id);
});
socket.on('ping', socket1 => {
socket.emit('pong')
});
});
});
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/products', productRouter);
app.use('/payment', paymentRouter)
app.get('/login', userAuth, function (req, res) {
res.send("login")
})
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;