-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
73 lines (65 loc) · 1.97 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
//making the server using express js::
const express = require("express");
const app = express();
const db = require("./Db");
const { error } = require("console");
const bodyParser = require("body-parser");
app.use(bodyParser.json());
//authetication:using session and cookies method::
const passport = require("passport");
const localStrategy = require("passport-local").Strategy;
passport.use(
new localStrategy(async (username, password, done) => {
try {
const user = await Person.findOne({ username: username });
if (!user) {
return done(null, false, { message: "Incorrect username" });
}
const isPasswordMatch = user.password == password ? true : false;
if (isPasswordMatch) {
return done(null, user);
} else {
return done(null, false, { message: "Incorrect password" });
}
} catch (err) {
return done(err);
}
})
);
app.use(passport.initialize());
app.get("/", passport.authenticate("local", { session: false }), (req, res) => {
res.send("working");
});
//middleware::
const logRequest = (req, res, next) => {
console.log(
`${new Date().toLocaleString()} Request Mode to : ${req.originalUrl} `
);
next();
};
app.use(logRequest);
const personRoutes = require("./routes/personRoutes");
const Person = require("./module/person");
//use the routers:
app.use("/person", personRoutes);
app.listen(3000, () => {
console.log("server is listening");
});
//basics fundamentals:
// const note = require("./notes");
// let _ = require("lodash");
// console.log("server file is created");
// console.log(note.age);
// console.log(note.addNumber(18, 10));
// let arr = [1, 2, 4, 1, 2, "first", "first"];
// console.log(_.uniq(arr));
// app.get("/", function (req, res) {
// res.send("hello world");
// });
// app.get("/chicken", function (req, res) {
// res.send("i love to have a chicken");
// });
// app.get("/idli", function (req, res) {
// res.send("South Indian food Is yUmmm");
// });
//extract personRoutes: