-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
113 lines (85 loc) · 2.65 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
// console.log("server log is running")
// function add(a,b) {
// return a+b;
// }
// var add = (a,b) => {return a+b};
// var result = add(2,4);
// console.log(result);
// var subtract = (a,b) => a-b;
// var result = subtract(9,4);
// console.log(result);
//Callback Function
// function callback() {
// console.log("callback function is running");
// }
// const add = function(a, b, callback) {
// var result = a + b;
// console.log("result " + result)
// callback();
// }
// add(2, 45, callback)
// Fs module
// https://nodejs.org/api
// var fs = require('fs');
// var os = require('os');
// var user = os.userInfo();
// console.log(user);
// console.log("welcome "+ user.username);
// fs.appendFile('greeting.txt','Hi ' + user.username + '!\n', () => {
// console.log("file created");
// } )
// Json to Object
// const jsonString = '{"name": "John", "age": 30, "city":"New York"}';
// const jsonobject = JSON.parse(jsonString) ;
// console.log(jsonobject.name);
// object to Json
// const objectToConvert = {
// name: "Alice",
// age: 25
// };
// const json = JSON.stringify(objectToConvert); // Convert object to JSm•4 string
// console. log(json)
// console.log(typeof json)
// Express
const express = require("express");
const app = express();
const db = require("./db");
require("dotenv").config();
const passport = require("./auth");
const PORT = process.env.PORT || 3000;
const bodyParser = require("body-parser");
app.use(bodyParser.json());
const menu = require("./models/menu");
//Callback Function - throw new MongooseError('Model.prototype.save() no longer accepts a callback');
// const data = req.body
// const newPerson = new Person(data);
// newPerson.save((error, savedperson) => {
// if (error) {
// console.log('Error saving person:', error);
// res.status(500).json({error: 'Error saving person'})
// }else{
// console.log('data saved succesfully');
// res.status(200).json(savedperson);
// }
// })
// Get method
// Middle Ware
const logRequest = (req, res, next) => {
console.log(
`[${new Date().toLocaleString()}] Request Made to: ${req.originalUrl}`
);
next();
};
app.use(logRequest);
app.use(passport.initialize());
const localAuthMiddleware = passport.authenticate("local", { session: false });
app.get("/", localAuthMiddleware, function (req, res) {
res.send("Welcome to Hotel");
});
const personRoutes = require("./routes/personRoutes");
app.use("/person", personRoutes);
const menuRoutes = require("./routes/menuRoutes");
app.use("/menu", menuRoutes);
app.listen(PORT, () => {
console.log("server is running");
});