Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
midterm_practical/app.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
67 lines (58 sloc)
2.28 KB
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
/** @format */ | |
import express from "express"; | |
import dirname from "path"; | |
import path from "path"; | |
import { openDb, addDog } from "./datasource.js"; | |
import { fileURLToPath } from "url"; | |
const app = express(); | |
const port = 3000; | |
// Middleware to parse JSON bodies and URL-encoded data | |
app.use(express.json()); | |
app.use(express.urlencoded({ extended: true })); | |
// Set EJS as the templating engine | |
app.set("view engine", "ejs"); | |
app.set("views", path.join(path.resolve(), "views")); | |
// Resolve path and name of current directory | |
const __fileName = fileURLToPath(import.meta.url); | |
const __dirName = path.dirname(__fileName); | |
app.use(express.static(__dirName + "/public")); | |
// Route to list all dogs using the view | |
app.get("/", async (req, res) => { | |
try { | |
const db = await openDb(); | |
const sql = "SELECT * FROM dogs"; | |
const rows = await db.all(sql); | |
res.render("dogs", { dogs: rows }); | |
} catch (err) { | |
res.status(400).json({ error: err.message }); | |
} | |
}); | |
// Route to add a new dog | |
app.post("/add_dog", async (req, res) => { | |
const { dogname, breed, age, shots, disposition, monthsinshelter } = req.body; | |
let is_shots = req.body.shots ? 1 : 0; | |
console.log(`IN ADD DOG - dogname ${dogname}, breed ${breed}, age ${age}, shots ${is_shots}, disposition ${disposition}, monthsinshelter ${monthsinshelter}`); | |
try{ | |
const db = await openDb(); | |
await db.run('INSERT INTO dogs (dogname, breed, age, shots, disposition, monthsinshelter) VALUES (?, ?, ?, ?, ?, ?)', [dogname, breed, age, is_shots, disposition, monthsinshelter]); | |
res.redirect('/'); | |
} catch (err) { | |
console.error(err); | |
res.status(500).send('An error occurred while submitting the form.'); | |
} | |
}); | |
// Route to delete a dog | |
app.delete("/delete/:id", async (req, res) => { | |
try { | |
const db = await openDb(); //waits for database function to run | |
await db.run('DELETE FROM dogs WHERE id = ?', req.params.id); //identifies id and selects that dog for deletion | |
res.redirect('/'); //redirect to home, basically reload page with updated info | |
} catch (error) { | |
console.error(error); | |
res.status(500).send('Error deleting dog.'); | |
} | |
}); | |
// Start the server | |
app.listen(port, () => { | |
console.log(`Server is running on http://localhost:${port}`); | |
}); |