-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
199 lines (161 loc) · 4.46 KB
/
app.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Import required modules
const express = require('express');
const mysql = require('mysql');
const bodyParser = require('body-parser');
const axios = require('axios');
const ejs = require('ejs');
const fs = require('fs');
// Create a new express app
const app = express();
// Use body-parser middleware to handle form data
// app.use(bodyParser.urlencoded({ extended: false }));
// MySQL Connection
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'expressdb'
});
db.connect((err) => {
if (err) {
throw err;
}
console.log('Connected to database');
});
// Parse JSON and URL-encoded query
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Set static folder
app.use(express.static(__dirname + '/public'));
var requestDate = function (req, res, next) {
req.requestDate = Date()
next()
}
app.use(requestDate);
// Set the view engine to ejs
app.set('view engine', 'ejs');
// Define routes
app.get('/', (req, res) => {
res.render('login');
});
app.get('/user', (req, res) => {
res.render('crud/users');
});
// Get User API
app.get('/api/users', (req, res) => {
const sql = 'SELECT * FROM users';
db.query(sql, (err, result) => {
if (err) {
throw err;
}
res.json(result);
});
});
// POST User API
app.post('/api/users', (req, res) => {
const { name, email } = req.body;
const sql = `INSERT INTO users (name, email) VALUES ('${name}', '${email}')`;
db.query(sql, (err, result) => {
if (err) {
throw err;
}
res.json(result);
});
});
// UPDATE User API
app.put('/api/users/:id', (req, res) => {
const id = req.params.id;
const { name, email } = req.body;
const sql = `UPDATE users SET name = '${name}', email = '${email}' WHERE id = ${id}`;
db.query(sql, (err, result) => {
if (err) {
throw err;
}
res.json(result);
});
});
// DELETE User API
app.delete('/api/users/:id', (req, res) => {
const id = req.params.id;
const sql = `DELETE FROM users WHERE id = ${id}`;
db.query(sql, (err, result) => {
if (err) {
throw err;
}
res.json(result);
});
});
// GET route to fetch all users
app.get('/users', async (req, res) => {
try {
const response = await axios.get('http://localhost:3000/api/users');
res.send(response.data);
} catch (error) {
console.error(error);
res.status(500).send('Error fetching users');
}
});
app.post('/submitlogin', (req, res) => {
const { username, password } = req.body;
if (username != "admin") {
res.render('error', { message: "Invalid Username, Please retry again" });
}else if (password != "admin") {
res.render('error', { message: "Invalid Password, Please retry again" });
}else{
res.render('home', { requestDate: req.requestDate, message: "Login Success !" });
}
});
app.get('/home', (req, res) => {
res.render('home', { requestDate: req.requestDate, message: "" });
});
app.get('/registration', (req, res) => {
res.render('registration', { requestDate: req.requestDate });
});
app.get('/services', (req, res) => {
res.render('services');
});
app.get('/contact', function(req, res) {
var data = {
lat: 3.0652,
lng: 101.4832
};
res.render('contact', { data: data, requestDate: req.requestDate });
});
app.get("/list_movies", (req, res) => {
fs.readFile(__dirname + '/' + 'json/movies.json', 'utf8', (err, data) => {
res.end(data);
});
});
app.get('/form', (req, res) => {
res.render('form');
});
// Handle form submission
app.post('/submit', (req, res) => {
const firstName = req.body.first_name;
const lastName = req.body.last_name;
if (firstName && lastName) {
res.send(`<h1>Thank you for submitting the form, ${firstName} ${lastName}!</h1>`);
} else {
res.send('<h1>Please enter your first name and last name.</h1>');
}
});
app.get('/marks', (req, res) => {
res.render('marks');
});
// POST route for /marks
app.post('/submitmarks', (req, res) => {
const { name, math, english, computer } = req.body;
const total = Number(math) + Number(english) + Number(computer);
res.render('marks', { total: total, math: math, english: english, computer: computer, name: name });
});
// FS Method
// app.get('/some/route', function(req, res) {
// fs.readFile('path/to/your/file.txt', 'utf8', function(err, data) {
// if (err) throw err;
// res.render('your-template', { fileContents: data });
// });
// });
// Start the server
app.listen(3000, () => {
console.log('Server started on port 3000');
});