-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (40 loc) · 1.4 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
const express = require('express');
const cookieParser = require('cookie-parser');
const path = require('path');
const app = express();
const PORT = 3000;
// Configure Express to use EJS as a templating engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Use cookieParser
app.use(cookieParser());
app.get('/public/bootstrap.min.css', function(req, res) {
res.setHeader('Content-Type', 'text/css');
res.sendFile(__dirname + '/public/bootstrap.min.css');
});
// Configure Express to parse form data
app.use(express.urlencoded({ extended: true }));
// Path to set the cookie
app.post('/setcookie', (req, res) => {
const cookieValue = req.body.cookieValue;
res.cookie('myCookie', cookieValue);
res.redirect('/');
});
// Path to delete the cookie
app.post('/deletecookie', (req, res) => {
res.clearCookie('myCookie');
res.redirect('/');
});
//Set the main route to display the page
app.get('/', (req, res) => {
// Get the value of the cookie 'myCookie'
const myCookie = req.cookies.myCookie || 'No cookie set';
// Define the 'error' variable as null
let error = null;
// Render the index.ejs page with the cookie value and the 'error' variable
res.render('index', { myCookie, error });
});
// Start the server
app.listen(PORT, () => {
console.log(`Server started on http://localhost:${PORT}`);
});