avatarciao 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/EJS/DynamicGreet.ejs +16 -0
- package/EJS/ErrorHandlingCustom.ejs +119 -0
- package/EJS/IncludeHeaderFooter.ejs +25 -0
- package/EJS/StudentDashboard.ejs +100 -0
- package/EJS/StudentDetailTable.ejs +62 -0
- package/EJS/paginationSystem.ejs +104 -0
- package/Express Session/index.js +155 -0
- package/ExpressModularRoutes/DynamicRouting.js +30 -0
- package/ExpressModularRoutes/MultiModuleBlogapi.js +36 -0
- package/ExpressModularRoutes/userRoutes.js +26 -0
- package/FS/basics.js +54 -0
- package/FS/copyFile.js +18 -0
- package/FS/periodicLogger.js +30 -0
- package/Http/BasicsOfHttp.js +73 -0
- package/Http/getPostHandling.js +44 -0
- package/Http/serveFiles.js +34 -0
- package/JWT/u1.js +78 -0
- package/JWT/u2.js +92 -0
- package/JWT/u3.js +97 -0
- package/JWT/u4.js +120 -0
- package/MongoDB/StudentManagementWithPagination.js +148 -0
- package/MongoDB/companyManagementSystem.js +139 -0
- package/Mongoose/Aggregate-E-commOrderAnalytics.js +287 -0
- package/Mongoose/EmployeePayrollSystem.js +104 -0
- package/Mongoose/EventManagementSystem.js +96 -0
- package/Multer/FileManagementApi.js +107 -0
- package/Multer/FileSize&TypeValidation.js +53 -0
- package/Multer/FileUploadFormData.js +77 -0
- package/Multer/MultiFileUpload.js +52 -0
- package/Multer/SingleFileUpload.js +58 -0
- package/express-routing/DynamicRoute.js +20 -0
- package/express-routing/StaticRouting.js +18 -0
- package/keys/all.txt +36 -0
- package/package.json +13 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<title>Greeting</title>
|
|
6
|
+
</head>
|
|
7
|
+
<body>
|
|
8
|
+
|
|
9
|
+
<% if (isLoggedIn) { %>
|
|
10
|
+
<h1>Welcome back, <%= userName %>!</h1>
|
|
11
|
+
<% } else { %>
|
|
12
|
+
<h2>Please log in to continue</h2>
|
|
13
|
+
<% } %>
|
|
14
|
+
|
|
15
|
+
</body>
|
|
16
|
+
</html>
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
const express = require("express");
|
|
2
|
+
const ejs = require("ejs");
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
|
|
6
|
+
const errorTemplate = `
|
|
7
|
+
<!DOCTYPE html>
|
|
8
|
+
<html>
|
|
9
|
+
<head>
|
|
10
|
+
<title>Error <%= errorData.statusCode %></title>
|
|
11
|
+
<style>
|
|
12
|
+
body {
|
|
13
|
+
font-family: Arial, sans-serif;
|
|
14
|
+
background: #f4f4f4;
|
|
15
|
+
padding: 40px;
|
|
16
|
+
}
|
|
17
|
+
.box {
|
|
18
|
+
background: #fff;
|
|
19
|
+
padding: 25px;
|
|
20
|
+
max-width: 600px;
|
|
21
|
+
margin: auto;
|
|
22
|
+
border-radius: 6px;
|
|
23
|
+
}
|
|
24
|
+
h1 {
|
|
25
|
+
color: #c0392b;
|
|
26
|
+
}
|
|
27
|
+
.info {
|
|
28
|
+
margin: 8px 0;
|
|
29
|
+
}
|
|
30
|
+
.suggestion {
|
|
31
|
+
margin-top: 20px;
|
|
32
|
+
padding: 12px;
|
|
33
|
+
background: #eef;
|
|
34
|
+
border-left: 4px solid #3498db;
|
|
35
|
+
}
|
|
36
|
+
a {
|
|
37
|
+
text-decoration: none;
|
|
38
|
+
color: #3498db;
|
|
39
|
+
font-weight: bold;
|
|
40
|
+
}
|
|
41
|
+
</style>
|
|
42
|
+
</head>
|
|
43
|
+
<body>
|
|
44
|
+
|
|
45
|
+
<div class="box">
|
|
46
|
+
<% if (errorData.errorType === "NOT_FOUND") { %>
|
|
47
|
+
<h1>Page not found</h1>
|
|
48
|
+
<% } else if (errorData.errorType === "SERVER_ERROR") { %>
|
|
49
|
+
<h1>Internal server error</h1>
|
|
50
|
+
<% } else if (errorData.errorType === "VALIDATION") { %>
|
|
51
|
+
<h1>Please check your form inputs</h1>
|
|
52
|
+
<% } %>
|
|
53
|
+
|
|
54
|
+
<div class="info"><strong>Status Code:</strong> <%= errorData.statusCode %></div>
|
|
55
|
+
<div class="info"><strong>Error Message:</strong> <%= errorData.message %></div>
|
|
56
|
+
<div class="info"><strong>Timestamp:</strong> <%= errorData.timestamp %></div>
|
|
57
|
+
<div class="info"><strong>Request URL:</strong> <%= errorData.requestUrl %></div>
|
|
58
|
+
|
|
59
|
+
<div class="suggestion">
|
|
60
|
+
<% if (errorData.errorType === "NOT_FOUND") { %>
|
|
61
|
+
<a href="/">Go back to Home Page</a>
|
|
62
|
+
<% } else if (errorData.errorType === "SERVER_ERROR") { %>
|
|
63
|
+
Try again later.
|
|
64
|
+
<% } else if (errorData.errorType === "VALIDATION") { %>
|
|
65
|
+
Fix the highlighted form inputs and resubmit.
|
|
66
|
+
<% } %>
|
|
67
|
+
</div>
|
|
68
|
+
</div>
|
|
69
|
+
|
|
70
|
+
</body>
|
|
71
|
+
</html>
|
|
72
|
+
`;
|
|
73
|
+
|
|
74
|
+
function renderError(res, errorData) {
|
|
75
|
+
const html = ejs.render(errorTemplate, { errorData });
|
|
76
|
+
res.status(errorData.statusCode).send(html);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
app.get("/", (req, res) => {
|
|
80
|
+
res.send("<h2>Home Page</h2>");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
app.get("/submit", (req, res) => {
|
|
84
|
+
renderError(res, {
|
|
85
|
+
errorType: "VALIDATION",
|
|
86
|
+
statusCode: 400,
|
|
87
|
+
message: "Invalid form data submitted",
|
|
88
|
+
timestamp: new Date(),
|
|
89
|
+
requestUrl: req.originalUrl
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
app.get("/crash", (req, res, next) => {
|
|
94
|
+
next(new Error("Unexpected failure"));
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
app.use((req, res) => {
|
|
98
|
+
renderError(res, {
|
|
99
|
+
errorType: "NOT_FOUND",
|
|
100
|
+
statusCode: 404,
|
|
101
|
+
message: "The requested page does not exist",
|
|
102
|
+
timestamp: new Date(),
|
|
103
|
+
requestUrl: req.originalUrl
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
app.use((err, req, res, next) => {
|
|
108
|
+
renderError(res, {
|
|
109
|
+
errorType: "SERVER_ERROR",
|
|
110
|
+
statusCode: 500,
|
|
111
|
+
message: err.message,
|
|
112
|
+
timestamp: new Date(),
|
|
113
|
+
requestUrl: req.originalUrl
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
app.listen(3000, () => {
|
|
118
|
+
console.log("Server running on port 3000");
|
|
119
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<title>My Website</title>
|
|
6
|
+
</head>
|
|
7
|
+
<body>
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
<header>
|
|
11
|
+
<h1>My Website</h1>
|
|
12
|
+
</header>
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
<main>
|
|
16
|
+
<p>Welcome to our site!</p>
|
|
17
|
+
</main>
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
<footer>
|
|
21
|
+
© 2025 My Website
|
|
22
|
+
</footer>
|
|
23
|
+
|
|
24
|
+
</body>
|
|
25
|
+
</html>
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<title>Student Dashboard</title>
|
|
6
|
+
<style>
|
|
7
|
+
body {
|
|
8
|
+
font-family: Arial, sans-serif;
|
|
9
|
+
padding: 20px;
|
|
10
|
+
}
|
|
11
|
+
.admin-badge {
|
|
12
|
+
display: inline-block;
|
|
13
|
+
background: gold;
|
|
14
|
+
color: black;
|
|
15
|
+
padding: 5px 10px;
|
|
16
|
+
margin-left: 10px;
|
|
17
|
+
border-radius: 4px;
|
|
18
|
+
font-weight: bold;
|
|
19
|
+
}
|
|
20
|
+
.course {
|
|
21
|
+
margin: 8px 0;
|
|
22
|
+
}
|
|
23
|
+
.grade-A {
|
|
24
|
+
color: green;
|
|
25
|
+
font-weight: bold;
|
|
26
|
+
}
|
|
27
|
+
.grade-B, .grade-C {
|
|
28
|
+
color: blue;
|
|
29
|
+
font-weight: bold;
|
|
30
|
+
}
|
|
31
|
+
.grade-other {
|
|
32
|
+
color: red;
|
|
33
|
+
font-weight: bold;
|
|
34
|
+
}
|
|
35
|
+
.notice {
|
|
36
|
+
margin-top: 20px;
|
|
37
|
+
padding: 10px;
|
|
38
|
+
background: #f4f4f4;
|
|
39
|
+
}
|
|
40
|
+
footer {
|
|
41
|
+
text-align: center;
|
|
42
|
+
margin-top: 30px;
|
|
43
|
+
color: #777;
|
|
44
|
+
}
|
|
45
|
+
</style>
|
|
46
|
+
</head>
|
|
47
|
+
<body>
|
|
48
|
+
|
|
49
|
+
<h2>
|
|
50
|
+
Welcome, <%= student.name %>
|
|
51
|
+
<% if (student.role === "admin") { %>
|
|
52
|
+
<span class="admin-badge">★ Admin Access</span>
|
|
53
|
+
<% } %>
|
|
54
|
+
</h2>
|
|
55
|
+
|
|
56
|
+
<p>Email: <%= student.email %></p>
|
|
57
|
+
|
|
58
|
+
<hr>
|
|
59
|
+
|
|
60
|
+
<h3>Your Courses</h3>
|
|
61
|
+
|
|
62
|
+
<% if (courses.length === 0) { %>
|
|
63
|
+
<p>No courses enrolled yet</p>
|
|
64
|
+
<% } else { %>
|
|
65
|
+
<ul>
|
|
66
|
+
<% courses.forEach(course => { %>
|
|
67
|
+
<li class="course">
|
|
68
|
+
<%= course.title %> -
|
|
69
|
+
<% if (course.grade === "A") { %>
|
|
70
|
+
<span class="grade-A"><%= course.grade %></span>
|
|
71
|
+
<% } else if (course.grade === "B" || course.grade === "C") { %>
|
|
72
|
+
<span class="grade-B"><%= course.grade %></span>
|
|
73
|
+
<% } else { %>
|
|
74
|
+
<span class="grade-other"><%= course.grade %></span>
|
|
75
|
+
<% } %>
|
|
76
|
+
</li>
|
|
77
|
+
<% }) %>
|
|
78
|
+
</ul>
|
|
79
|
+
<% } %>
|
|
80
|
+
|
|
81
|
+
<hr>
|
|
82
|
+
|
|
83
|
+
<div class="notice">
|
|
84
|
+
<p>Escaped Output:</p>
|
|
85
|
+
<%= notice %>
|
|
86
|
+
</div>
|
|
87
|
+
|
|
88
|
+
<div class="notice">
|
|
89
|
+
<p>Unescaped Output:</p>
|
|
90
|
+
<%- notice %>
|
|
91
|
+
</div>
|
|
92
|
+
|
|
93
|
+
<hr>
|
|
94
|
+
|
|
95
|
+
<footer>
|
|
96
|
+
<p>© 2026 Student Dashboard System</p>
|
|
97
|
+
</footer>
|
|
98
|
+
|
|
99
|
+
</body>
|
|
100
|
+
</html>
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<title>Student Report</title>
|
|
6
|
+
<style>
|
|
7
|
+
table {
|
|
8
|
+
border-collapse: collapse;
|
|
9
|
+
width: 60%;
|
|
10
|
+
margin: 20px auto;
|
|
11
|
+
font-family: Arial, sans-serif;
|
|
12
|
+
}
|
|
13
|
+
th, td {
|
|
14
|
+
border: 1px solid #ccc;
|
|
15
|
+
padding: 10px;
|
|
16
|
+
text-align: center;
|
|
17
|
+
}
|
|
18
|
+
th {
|
|
19
|
+
background-color: #f4f4f4;
|
|
20
|
+
}
|
|
21
|
+
.low-marks {
|
|
22
|
+
color: red;
|
|
23
|
+
font-weight: bold;
|
|
24
|
+
}
|
|
25
|
+
</style>
|
|
26
|
+
</head>
|
|
27
|
+
<body>
|
|
28
|
+
|
|
29
|
+
<h2 style="text-align: center;">Student Report</h2>
|
|
30
|
+
|
|
31
|
+
<table>
|
|
32
|
+
<thead>
|
|
33
|
+
<tr>
|
|
34
|
+
<th>Name</th>
|
|
35
|
+
<th>Math</th>
|
|
36
|
+
<th>Science</th>
|
|
37
|
+
<th>English</th>
|
|
38
|
+
</tr>
|
|
39
|
+
</thead>
|
|
40
|
+
<tbody>
|
|
41
|
+
<% students.forEach(student => { %>
|
|
42
|
+
<tr>
|
|
43
|
+
<td><%= student.name %></td>
|
|
44
|
+
|
|
45
|
+
<td class="<%= student.marks.math < 70 ? 'low-marks' : '' %>">
|
|
46
|
+
<%= student.marks.math %>
|
|
47
|
+
</td>
|
|
48
|
+
|
|
49
|
+
<td class="<%= student.marks.science < 70 ? 'low-marks' : '' %>">
|
|
50
|
+
<%= student.marks.science %>
|
|
51
|
+
</td>
|
|
52
|
+
|
|
53
|
+
<td class="<%= student.marks.english < 70 ? 'low-marks' : '' %>">
|
|
54
|
+
<%= student.marks.english %>
|
|
55
|
+
</td>
|
|
56
|
+
</tr>
|
|
57
|
+
<% }) %>
|
|
58
|
+
</tbody>
|
|
59
|
+
</table>
|
|
60
|
+
|
|
61
|
+
</body>
|
|
62
|
+
</html>
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
const express = require("express");
|
|
2
|
+
const ejs = require("ejs");
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
|
|
6
|
+
app.get("/", (req, res) => {
|
|
7
|
+
const page = parseInt(req.query.page) || 1;
|
|
8
|
+
const limit = 5;
|
|
9
|
+
|
|
10
|
+
const dataJSON = `
|
|
11
|
+
[
|
|
12
|
+
{ "id": 1, "name": "Product 1" },
|
|
13
|
+
{ "id": 2, "name": "Product 2" },
|
|
14
|
+
{ "id": 3, "name": "Product 3" },
|
|
15
|
+
{ "id": 4, "name": "Product 4" },
|
|
16
|
+
{ "id": 5, "name": "Product 5" },
|
|
17
|
+
{ "id": 6, "name": "Product 6" },
|
|
18
|
+
{ "id": 7, "name": "Product 7" },
|
|
19
|
+
{ "id": 8, "name": "Product 8" },
|
|
20
|
+
{ "id": 9, "name": "Product 9" },
|
|
21
|
+
{ "id": 10, "name": "Product 10" },
|
|
22
|
+
{ "id": 11, "name": "Product 11" },
|
|
23
|
+
{ "id": 12, "name": "Product 12" },
|
|
24
|
+
{ "id": 13, "name": "Product 13" },
|
|
25
|
+
{ "id": 14, "name": "Product 14" },
|
|
26
|
+
{ "id": 15, "name": "Product 15" },
|
|
27
|
+
{ "id": 16, "name": "Product 16" },
|
|
28
|
+
{ "id": 17, "name": "Product 17" },
|
|
29
|
+
{ "id": 18, "name": "Product 18" },
|
|
30
|
+
{ "id": 19, "name": "Product 19" },
|
|
31
|
+
{ "id": 20, "name": "Product 20" }
|
|
32
|
+
]
|
|
33
|
+
`;
|
|
34
|
+
|
|
35
|
+
const products = JSON.parse(dataJSON);
|
|
36
|
+
|
|
37
|
+
const totalPages = Math.ceil(products.length / limit);
|
|
38
|
+
const start = (page - 1) * limit;
|
|
39
|
+
const paginatedProducts = products.slice(start, start + limit);
|
|
40
|
+
|
|
41
|
+
const template = `
|
|
42
|
+
<!DOCTYPE html>
|
|
43
|
+
<html>
|
|
44
|
+
<head>
|
|
45
|
+
<title>Product Catalog</title>
|
|
46
|
+
<style>
|
|
47
|
+
body { font-family: Arial; padding: 20px; }
|
|
48
|
+
ul { list-style: none; padding: 0; }
|
|
49
|
+
li { margin: 6px 0; }
|
|
50
|
+
.pagination a, .pagination span {
|
|
51
|
+
margin: 0 5px;
|
|
52
|
+
padding: 6px 12px;
|
|
53
|
+
border: 1px solid #ccc;
|
|
54
|
+
text-decoration: none;
|
|
55
|
+
}
|
|
56
|
+
.disabled {
|
|
57
|
+
color: #aaa;
|
|
58
|
+
border-color: #eee;
|
|
59
|
+
pointer-events: none;
|
|
60
|
+
}
|
|
61
|
+
</style>
|
|
62
|
+
</head>
|
|
63
|
+
<body>
|
|
64
|
+
|
|
65
|
+
<h2>Product Catalog</h2>
|
|
66
|
+
|
|
67
|
+
<ul>
|
|
68
|
+
<% products.forEach(p => { %>
|
|
69
|
+
<li><%= p.id %> - <%= p.name %></li>
|
|
70
|
+
<% }) %>
|
|
71
|
+
</ul>
|
|
72
|
+
|
|
73
|
+
<p>Page <%= currentPage %> of <%= totalPages %></p>
|
|
74
|
+
|
|
75
|
+
<div class="pagination">
|
|
76
|
+
<% if (currentPage > 1) { %>
|
|
77
|
+
<a href="/?page=<%= currentPage - 1 %>">Previous</a>
|
|
78
|
+
<% } else { %>
|
|
79
|
+
<span class="disabled">Previous</span>
|
|
80
|
+
<% } %>
|
|
81
|
+
|
|
82
|
+
<% if (currentPage < totalPages) { %>
|
|
83
|
+
<a href="/?page=<%= currentPage + 1 %>">Next</a>
|
|
84
|
+
<% } else { %>
|
|
85
|
+
<span class="disabled">Next</span>
|
|
86
|
+
<% } %>
|
|
87
|
+
</div>
|
|
88
|
+
|
|
89
|
+
</body>
|
|
90
|
+
</html>
|
|
91
|
+
`;
|
|
92
|
+
|
|
93
|
+
const html = ejs.render(template, {
|
|
94
|
+
products: paginatedProducts,
|
|
95
|
+
currentPage: page,
|
|
96
|
+
totalPages
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
res.send(html);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
app.listen(3000, () => {
|
|
103
|
+
console.log("Server running on http://localhost:3000");
|
|
104
|
+
});
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
const express = require("express");
|
|
2
|
+
const session = require("express-session");
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
const PORT = 3000;
|
|
8
|
+
|
|
9
|
+
app.use(
|
|
10
|
+
session({
|
|
11
|
+
secret: "session_secret_key",
|
|
12
|
+
resave: false,
|
|
13
|
+
saveUninitialized: false,
|
|
14
|
+
cookie: {
|
|
15
|
+
maxAge: 30 * 60 * 1000
|
|
16
|
+
}
|
|
17
|
+
})
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
app.use(express.urlencoded({ extended: true }));
|
|
21
|
+
app.use(express.json());
|
|
22
|
+
|
|
23
|
+
const users = JSON.parse(
|
|
24
|
+
fs.readFileSync(path.join(__dirname, "user.json"), "utf-8")
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
function isAuthenticated(req, res, next) {
|
|
28
|
+
if (req.session.user) {
|
|
29
|
+
return next();
|
|
30
|
+
}
|
|
31
|
+
res.redirect("/login?expired=true");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isAdmin(req, res, next) {
|
|
35
|
+
if (req.session.user && req.session.user.role === "admin") {
|
|
36
|
+
return next();
|
|
37
|
+
}
|
|
38
|
+
res.status(403).send("Access Denied");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
app.get("/login", (req, res) => {
|
|
42
|
+
const message = req.query.expired
|
|
43
|
+
? "<p style='color:red;'>Session expired. Please login again.</p>"
|
|
44
|
+
: "";
|
|
45
|
+
|
|
46
|
+
res.send(`
|
|
47
|
+
<h2>Login</h2>
|
|
48
|
+
${message}
|
|
49
|
+
<form method="POST" action="/login">
|
|
50
|
+
<input type="email" name="username" placeholder="Email" required /><br><br>
|
|
51
|
+
<input type="password" name="password" placeholder="Password" required /><br><br>
|
|
52
|
+
<button type="submit">Login</button>
|
|
53
|
+
</form>
|
|
54
|
+
`);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
app.post("/login", (req, res) => {
|
|
58
|
+
const { username, password } = req.body;
|
|
59
|
+
|
|
60
|
+
const user = users.find(
|
|
61
|
+
(u) => u.username === username && u.password === password
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
if (!user) {
|
|
65
|
+
return res.send("<h3>Invalid credentials</h3><a href='/login'>Try Again</a>");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
req.session.user = {
|
|
69
|
+
username: user.username,
|
|
70
|
+
role: user.role,
|
|
71
|
+
loginTime: Date.now()
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
if (user.role === "admin") {
|
|
75
|
+
res.redirect("/adminhome");
|
|
76
|
+
} else {
|
|
77
|
+
res.redirect("/userhome");
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
app.get("/adminhome", isAuthenticated, isAdmin, (req, res) => {
|
|
82
|
+
res.send(`
|
|
83
|
+
<h2>Admin Home</h2>
|
|
84
|
+
<p>Welcome ${req.session.user.username}</p>
|
|
85
|
+
<a href="/admin/panel">View Active Sessions</a><br>
|
|
86
|
+
<a href="/profile">My Profile</a><br>
|
|
87
|
+
<a href="/logout">Logout</a>
|
|
88
|
+
`);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
app.get("/userhome", isAuthenticated, (req, res) => {
|
|
92
|
+
res.send(`
|
|
93
|
+
<h2>User Home</h2>
|
|
94
|
+
<p>Welcome ${req.session.user.username}</p>
|
|
95
|
+
<a href="/profile">My Profile</a><br>
|
|
96
|
+
<a href="/logout">Logout</a>
|
|
97
|
+
`);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
app.get("/profile", isAuthenticated, (req, res) => {
|
|
101
|
+
const timeSinceLogin = Math.floor(
|
|
102
|
+
(Date.now() - req.session.user.loginTime) / 1000
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
res.send(`
|
|
106
|
+
<h2>Profile</h2>
|
|
107
|
+
<p><b>Username:</b> ${req.session.user.username}</p>
|
|
108
|
+
<p><b>Role:</b> ${req.session.user.role}</p>
|
|
109
|
+
<p><b>Time Since Login:</b> ${timeSinceLogin} seconds</p>
|
|
110
|
+
<p><b>Session ID:</b> ${req.sessionID}</p>
|
|
111
|
+
<a href="/logout">Logout</a>
|
|
112
|
+
`);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
app.get("/admin/panel", isAuthenticated, isAdmin, (req, res) => {
|
|
116
|
+
const store = req.sessionStore.sessions;
|
|
117
|
+
let rows = "";
|
|
118
|
+
|
|
119
|
+
Object.keys(store).forEach((sid) => {
|
|
120
|
+
const sessionData = JSON.parse(store[sid]);
|
|
121
|
+
if (sessionData.user) {
|
|
122
|
+
rows += `
|
|
123
|
+
<tr>
|
|
124
|
+
<td>${sessionData.user.username}</td>
|
|
125
|
+
<td>${new Date(sessionData.user.loginTime).toLocaleString()}</td>
|
|
126
|
+
<td>${new Date(sessionData.cookie.expires).toLocaleString()}</td>
|
|
127
|
+
</tr>
|
|
128
|
+
`;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
res.send(`
|
|
133
|
+
<h2>Admin Panel - Active Sessions</h2>
|
|
134
|
+
<table border="1" cellpadding="5">
|
|
135
|
+
<tr>
|
|
136
|
+
<th>Username</th>
|
|
137
|
+
<th>Login Time</th>
|
|
138
|
+
<th>Session Expiry</th>
|
|
139
|
+
</tr>
|
|
140
|
+
${rows}
|
|
141
|
+
</table>
|
|
142
|
+
<br>
|
|
143
|
+
<a href="/adminhome">Back</a>
|
|
144
|
+
`);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
app.get("/logout", (req, res) => {
|
|
148
|
+
req.session.destroy(() => {
|
|
149
|
+
res.redirect("/login");
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
app.listen(PORT, () => {
|
|
154
|
+
console.log(`Server running on http://localhost:${PORT}`);
|
|
155
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
const app = express();
|
|
3
|
+
|
|
4
|
+
app.use(express.json());
|
|
5
|
+
|
|
6
|
+
const products = [
|
|
7
|
+
{ id: '1', category: 'electronics', name: 'Laptop' },
|
|
8
|
+
{ id: '2', category: 'electronics', name: 'Phone' },
|
|
9
|
+
{ id: '1', category: 'fashion', name: 'Shirt' },
|
|
10
|
+
{ id: '2', category: 'fashion', name: 'Shoes' }
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
app.get('/api/products', (req, res) => {
|
|
14
|
+
res.json(products);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
app.get('/api/products/:category/:id', (req, res) => {
|
|
18
|
+
const { category, id } = req.params;
|
|
19
|
+
const product = products.find(
|
|
20
|
+
p => p.category === category && p.id === id
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
if (!product) {
|
|
24
|
+
return res.status(404).json({ message: 'Product not found' });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
res.json(product);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
app.listen(3000);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
const app = express();
|
|
3
|
+
|
|
4
|
+
app.use(express.json());
|
|
5
|
+
|
|
6
|
+
const posts = [
|
|
7
|
+
{ id: '1', title: 'First Post' },
|
|
8
|
+
{ id: '2', title: 'Second Post' }
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
const comments = [
|
|
12
|
+
{ id: '1', text: 'Great post!' },
|
|
13
|
+
{ id: '2', text: 'Very helpful' }
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
app.get('/api/posts', (req, res) => {
|
|
17
|
+
res.json(posts);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
app.get('/api/posts/:postId', (req, res) => {
|
|
21
|
+
const post = posts.find(p => p.id === req.params.postId);
|
|
22
|
+
if (!post) return res.status(404).json({ message: 'Post not found' });
|
|
23
|
+
res.json(post);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
app.get('/api/comments', (req, res) => {
|
|
27
|
+
res.json(comments);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
app.get('/api/comments/:commentId', (req, res) => {
|
|
31
|
+
const comment = comments.find(c => c.id === req.params.commentId);
|
|
32
|
+
if (!comment) return res.status(404).json({ message: 'Comment not found' });
|
|
33
|
+
res.json(comment);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
app.listen(3000);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
const app = express();
|
|
3
|
+
|
|
4
|
+
const users = [
|
|
5
|
+
{ id: '1', name: 'Alice', age: 25 },
|
|
6
|
+
{ id: '2', name: 'Bob', age: 30 },
|
|
7
|
+
{ id: '3', name: 'Charlie', age: 22 }
|
|
8
|
+
];
|
|
9
|
+
|
|
10
|
+
const usersRouter = express.Router();
|
|
11
|
+
|
|
12
|
+
usersRouter.get('/users', (req, res) => {
|
|
13
|
+
res.json(users);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
usersRouter.get('/users/:id', (req, res) => {
|
|
17
|
+
const user = users.find(u => u.id === req.params.id);
|
|
18
|
+
if (!user) {
|
|
19
|
+
return res.status(404).json({ message: 'User not found' });
|
|
20
|
+
}
|
|
21
|
+
res.json(user);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
app.use('/api', usersRouter);
|
|
25
|
+
|
|
26
|
+
app.listen(3000);
|