- Introduced sqlstring module for SQL escape and formatting. - Added supports-color module to detect terminal color support. - Created dashboard and login views with Bootstrap styling. - Implemented user schema in SQL with mock data for testing.
57 lines
1.3 KiB
JavaScript
57 lines
1.3 KiB
JavaScript
// static variables
|
|
import express from "express";
|
|
const app = express();
|
|
const port = 4000;
|
|
|
|
import { loginUser } from "./database.js";
|
|
|
|
app.use(express.urlencoded({ extended: true }));
|
|
app.set("view engine", "ejs");
|
|
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
app.listen(port, () => {
|
|
console.log(`Server is running on http://localhost:${port}`);
|
|
});
|
|
|
|
// Middleware to parse JSON bodies
|
|
app.use(express.json());
|
|
|
|
// Middleware to serve static files from the 'public' directory
|
|
app.use(express.static("public"));
|
|
|
|
// Main code below
|
|
|
|
// Route to handle GET requests to the root URL
|
|
app.get("/", (req, res) => {
|
|
res.render("login.ejs", { error: null, reload: false });
|
|
});
|
|
|
|
// Route to handle user login
|
|
app.post("/login", (req, res) => {
|
|
loginUser(req.body.username, req.body.password).then((result) => {
|
|
if (result.success) {
|
|
res
|
|
.status(200)
|
|
.render("dashboard.ejs", {
|
|
sqlResult: result,
|
|
newLink: `/dashboard/${result.user.id}`,
|
|
});
|
|
} else {
|
|
res
|
|
.status(401)
|
|
.render("login.ejs", { error: result.message, reload: true });
|
|
}
|
|
});
|
|
});
|
|
|
|
// error handling code
|
|
app.use((err, req, res, next) => {
|
|
console.error(err.stack);
|
|
res.status(500).send("Something broke!");
|
|
});
|