-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (46 loc) · 1.3 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
// Require the express module
const express = require('express');
// Create an express application
const app = express();
// Set the port number
const PORT = 3000;
// Middleware to parse URL-encoded bodies
app.use(express.urlencoded({ extended: true }));
// Array to store tasks
let tasks = [];
// Define routes
// Route to the home page
app.get('/', (req, res) => {
// Send a simple HTML response with a link to the tasks page
res.send(`
<html>
<head>
<title>Welcome to the To-Do List App!</title>
</head>
<body>
<h1>Welcome to the To-Do List App!</h1>
<p>Click <a href="/tasks">here</a> to view your tasks.</p>
</body>
</html>
`);
});
// Route to display tasks
app.get('/tasks', (req, res) => {
// Render the 'tasks' view (tasks.ejs) and pass the tasks array to it
res.render('tasks', { tasks: tasks });
});
// Route to add a new task
app.post('/tasks', (req, res) => {
// Extract the task from the request body
const task = req.body.task;
// Add the task to the tasks array
tasks.push(task);
// Redirect the user back to the tasks page
res.redirect('/tasks');
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
// Set EJS as the view engine
app.set('view engine', 'ejs');