-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.js
127 lines (106 loc) · 3.32 KB
/
Server.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
const express = require('express');
const path = require('path');
const expressLayout = require('express-ejs-layouts');
const bodyParser = require('body-parser');
const app = express();
const fs = require('fs');
const { name } = require('ejs');
//Set up body Parser Middleware
app.use(bodyParser.urlencoded({ extended:true}));
// Set EJS as the view engine
app.use(expressLayout);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, "Public",'views'));
app.set('layout', './layouts/web.ejs'); // Specify layout file
// Serve static files (HTML, CSS, JS)
app.use(express.static(path.join(__dirname, 'Public')));
// Define route to render index page
app.get('/', (req, res) => {
res.render('index',{
title : "Home"
});
});
// Serve JavaScript file dynamically
app.get('/index.js', (req, res) => {
// Instead of sending the actual JavaScript code,
// you can send a minimized version or even obfuscated version.
res.sendFile(path.join(__dirname, 'Public', 'index.js'));
});
app.get('/about', (req, res) => {
res.render('about',{
title : "about"
});
});
app.get('/project', (req, res) => {
res.render('Project',{
title : "about"
});
});
app.get('/services', (req, res) => {
res.render('Service',{
title : "Service"
});
});
app.get('/portfolio', (req, res) => {
res.render('about',{
title : "services"
});
});
app.get('/contact', (req, res) => {
res.render('Contact',{
title : "contact"
});
});
app.post('/contac',(req,res)=>{
const {name , email, message}=req.body;
const filePath = path.join(__dirname,'contact.txt');
fs.appendFile(filePath, "Name :- " + name + '\n' +
"email :- " + email + '\n'+ "Message :- " + message + '\n', (err)=>{
if(err){
console.error(`error saving message:`,err);
return res.status(500).send('Internal Server Error');
}
console.log('Name:-'+ name+ '\n'+ 'Gmail :-'+ email+'\n'+'message'+message +'\n');
console.log('Message Saved :',message);
res.send(`<div>
<h1>Thanks ${name}, We will Contact as soon as possible </h1>
<h2>We get your message : ${message}</h2>
<div>Go Back</div>
<a href="/">go back to the home page</a>
</div>`)
})
});
// Start the server
const PORT = process.env.PORT || 3002;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
/*
const express = require('express');
const path = require('path');
const expressLayouts = require('express-ejs-layouts');
const app = express();
// Set EJS as the view engine
app.set('view engine', 'ejs');
app.use(expressLayouts);
app.set('views', path.join(__dirname,"Public", 'views'));
// Serve static files (HTML, CSS, JS)
app.use(express.static(path.join(__dirname, 'Public')));
// Define routes
app.get('/', (req, res) => {
res.render('layout', {
title: 'Home'
});
});
app.get('/about', (req, res) => {
res.render('layout', {
title: 'About'
});
});
// Add more routes as needed
// Start the server
const PORT = process.env.PORT || 3002;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
*/