-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7afbe89
commit f47a560
Showing
1 changed file
with
50 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,76 @@ | ||
/* | ||
* Setup for the Express app | ||
* Express App Setup | ||
*/ | ||
// Imports necessary modules | ||
// Import necessary modules | ||
const express = require("express"); | ||
const path = require('path'); | ||
const app = express(); | ||
const data = require("./data.json"); | ||
// view engine to Pug and Serves static files from the public directory | ||
const data = require("./data.json"); | ||
|
||
// Set view engine to Pug and serve static files from the 'public' directory | ||
app.set("view engine", "pug"); | ||
app.use('/static',express.static(path.join(__dirname,'public'))); | ||
app.use('/static', express.static(path.join(__dirname, 'public'))); | ||
|
||
/* | ||
* Route Handlers | ||
*/ | ||
// Route for the homepage | ||
// Homepage route | ||
app.get("/", (req, res) => { | ||
res.render("index", { projects: data.projects }); | ||
}); | ||
// Route for the about page | ||
app.get("/about", (req,res) => { | ||
res.render('about'); | ||
res.render("index", { projects: data.projects }); | ||
}); | ||
|
||
// About page route | ||
app.get("/about", (req, res) => { | ||
res.render("about"); | ||
}); | ||
// Route for displaying project details | ||
|
||
// Route for displaying project details based on project ID | ||
app.get('/projects/:id', (req, res, next) => { | ||
const projectId = req.params.id; | ||
const project = data.projects.find(p => p.id == projectId); | ||
|
||
if (project) { | ||
res.render('project', { project }); | ||
res.render('project', { project }); | ||
} else { | ||
const err = new Error('Project not found'); | ||
err.status = 404; | ||
next(err); | ||
const err = new Error('Project not found'); | ||
err.status = 404; | ||
next(err); | ||
} | ||
}); | ||
}); | ||
|
||
/* | ||
* Error Handlers | ||
*/ | ||
// 404 Handler | ||
// 404 error handler for undefined routes | ||
app.use((req, res, next) => { | ||
const err = new Error('Sorry this Page is Not Found'); | ||
const err = new Error('Sorry, this page is not found'); | ||
err.status = 404; | ||
next(err); | ||
}); | ||
// Global Error Handler | ||
app.use((err, req, res, next) => { | ||
|
||
console.log(`Error Status: ${err.status || 500}`); | ||
console.log(`Error Message: ${err.message}`); | ||
res.status(err.status|| 500); | ||
res.render('page not found',{ | ||
message: "Oops Unfortunately We are unable to locate this page!!!", | ||
error: {} | ||
}); | ||
|
||
// Global error handler for all errors | ||
app.use((err, req, res, next) => { | ||
const status = err.status || 500; | ||
console.error(`Error Status: ${status}`); | ||
console.error(`Error Message: ${err.message}`); | ||
|
||
res.status(status); | ||
|
||
// Show a custom error message based on the status | ||
const errorMessage = status === 404 | ||
? "Oops! Unfortunately, we are unable to locate this page!" | ||
: "Something went wrong on our end!"; | ||
|
||
res.render('error', { | ||
message: errorMessage, | ||
error: status === 500 ? err.message : {} // only show error message if 500 | ||
}); | ||
}); | ||
}); | ||
|
||
/* | ||
* Starting the server | ||
* Start the server | ||
*/ | ||
app.listen(3000,() => { | ||
console.log("Port:3000 Running"); | ||
}); | ||
const PORT = process.env.PORT || 3000; | ||
app.listen(PORT, () => { | ||
console.log(`Server is running on port ${PORT}`); | ||
}); |