-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (53 loc) · 1.79 KB
/
index.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
require("dotenv").config();
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const { User } = require("./schemas/users");
const session = require("express-session");
const passport = require("passport");
const path = require("path");
const Queue = require("bull");
const { sendNotification } = require("./services/notificationHelpers");
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(
session({
//initialized session
secret: "our little secret",
resave: false,
saveUninitialized: true,
})
);
app.use(passport.initialize());
app.use(passport.session()); //for dealing with the session
passport.use(User.createStrategy()); //local strategy to authenticate users using their username and password
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
mongoose.connect(process.env.DATABASE_URL);
const db = mongoose.connection;
db.on("error", (error) => console.log(error));
db.once("open", () => console.log("Connected to Database"));
const uploadDir = path.join(__dirname, "uploads");
app.use("/uploads", express.static(uploadDir));
//app.use(express.static("public"));
const taskQueue = new Queue("My Queue");
taskQueue.process(async (job) => {
try {
await sendNotification(job.data.userID, job.data.taskTitle);
} catch (err) {
console.log(err);
}
});
taskQueue.on("completed", (job, result) => {
console.log(`Job completed with result ${result}`);
});
module.exports = { uploadDir, taskQueue };
const usersRouter = require("./routes/users");
const tasksRouter = require("./routes/tasks");
app.use("/users", usersRouter);
app.use("/tasks", tasksRouter);
app.listen(3000, () => console.log("Server Started"));