-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (126 loc) · 4.51 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
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
133
134
135
136
137
138
139
140
141
142
143
// color code- #f66 | text color - #424040 | normal- #fff
const express = require("express"); //server framework
const bodyparser = require("body-parser");//packages body-parser
const app = express(); //express method
const path = require("path");
const cookieParser = require('cookie-parser');
const expressSession = require('express-session');
const flash = require('express-flash');
//--------------------------------------
const userRoute = require('./route/userroute');//userroute for url path
const adminRoute = require('./route/adminroute');//adminroute url
//------------------------
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS,PUT,PATCH,DELETE');
res.setHeader("Access-Control-Allow-Headers", "content-type,X-Requested-With,authorization,ERR_HTTP_HEADERS_SENT");
next();
});
app.set("views", __dirname + "/views");//set view folder
app.set("view engine", "ejs");//set the view engine as ejs
app.use(bodyparser.urlencoded({ extended: true })); //multipart data for image video giving true
app.use(bodyparser.json());
app.use(express.static(path.join(__dirname, "resources")));//hosting public folder
// -- SESSION CODE BELOW START
const HOURS = 1000 * 60 * 60 * 24;
const {
PORT = process.env.PORT,
SESS_NAME = 'sid',
SESS_SECRET = 'JHGF>,./?;;LJ8#$?,KL:>>>,,KJJJDHE',
NODE_ENV = 'development',
SESS_LIFETIME = HOURS
} = process.env
const IN_PROD = NODE_ENV === 'production'
app.use(cookieParser());
app.use(expressSession({
// genid: function (req) {
// return i++ // use UUIDs for session IDs
// },
name: SESS_NAME,
resave: false,
secret: 'max',
saveUninitialized: false,
cookie: { maxAge: SESS_LIFETIME, sameSite: true, secure: IN_PROD }
}))
app.use(flash());
app.use((req, res, next) => {
res.locals.session = req.session;
res.locals.carttemp = req.cookies.carttemp;
const { customerId, customerEmail, token } = res.locals.session;
if (token) {
res.locals.custid = customerId;
res.locals.custemail = customerEmail;
}
next();
})
// app.use(function (req, res, next) {
// res.locals.carttemp = req.cookies.carttemp;
// next();
// });
//----route defination
app.get("/", (req, res) => {
const { userId } = req.session;
const wt = "Yummy Cake";
const redirect = [{
title: "WARNING!!! Redirect to login: [server address: localhost:1234/, port: 1234]",
login: "http://localhost:1234/user/login",
subtitle: "this is root page!!!"
// userid: `${userId}`
}];
res.render('welcome', { wt, redirect, userId });
});
//---------SESSION CODE END
app.use("/user", userRoute);
app.use("/admin", adminRoute);
//---error defining
app.use((err, req, res, next) => {
res.locals.error = err;
if (err.status >= 100 && err.status < 600) res.status(err.status);
else res.status(500);
return res.send({ "status": err.status, "message": err.message });
});
//--------------------------
//route for upload folders
//Serves all the request which includes /images in the url from Images folder
var publicDir = require('path').join(__filename, '/resources/uploads');
app.use(express.static(publicDir));
app.use(express.static('public'));
app.use('/upload', express.static(__dirname + '/resources/uploads'));
app.get("/upload", function (req, res, next) {
res.send(publicDir)
})
//end of route for upload folders
//route for giftuploads folders
//Serves all the request which includes /images in the url from Images folder
var giftuploadDir = require('path').join(__filename, '/resources/giftuploads');
app.use(express.static(giftuploadDir));
app.use(express.static('public'));
app.use('/giftuploads', express.static(__dirname + '/resources/giftuploads'));
app.get("/giftuploads", function (req, res, next) {
res.send(giftuploadDir)
})
//end of route for giftuploads folders
//ignore favicon problem
function ignoreFavicon(req, res, next) {
if (req.originalUrl == './favicon.ico') {
res.status(204).json({ nope: true });
} else if (req.originalUrl == '/map-red.png') {
res.status(204).json({ nope: true });
} else if (req.originalUrl == '/Roboto-Bold.woff2') {
res.status(204).json({ nope: true });
} else {
next();
}
}
app.use(ignoreFavicon);
//----------------------------------------------
// const port =1234;//set port
// curl -X GET http://localhost:1234/user/login -v
app.listen(PORT, () => {
try {
console.log(`server running on port: ${PORT}`)
} catch (err) {
console.log(`server not running on port: ${PORT} `)
}
});//port listen function
module.exports = app;