diff --git a/app.js b/app.js index 62262017326941f5c7c5fbe95551214002d02b1a..e1232f3b11156193f35770bef5ee0943a9cc84f9 100644 --- a/app.js +++ b/app.js @@ -1,19 +1,33 @@ const path = require('path'); const express = require('express'); const app = express(); -module.exports = app; - +const db = require('./db/Database.js'); //Configuration const viewsPath = path.join(__dirname, 'views'); -app.set("views", viewsPath) +app.set("views", viewsPath); app.set("view engine", "ejs"); //chemin d'accès app.get("/", defaut).get("/accueil",defaut); -app.all("*", (req, res) => res.status(404).send("<h1>Il semblerait que cette page n'existe pas.</h1>")) +app.get("/db/:collection", collection).get("/db", dbAdmin); +app.all("*", (req, res) => res.status(404).send("<h1>Il semblerait que cette page n'existe pas.</h1>")); function defaut(req, res){ const ind = {method : req.method, url : req.url} res.render("index",ind); -} \ No newline at end of file +} + +async function collection(req, res){ + const nomCollection = req.params.collection; + const data = await db.find(nomCollection, {}); + res.render("db/collection", {nomCollection, data}); +} + +async function dbAdmin(req, res){ + const collections = await db.listCollections(); + res.render("db/admin", {collections}); +} + + +module.exports = app; \ No newline at end of file diff --git a/server.js b/server.js index 83574b00c08e64a682395e8876f5c32ec0071f2d..7d32a0482660e4b32dc1d739f92073c7faf31ebc 100644 --- a/server.js +++ b/server.js @@ -1,9 +1,6 @@ const http = require('http'); const app = require('./app.js'); -const Database = require('./db/Database.js'); - -db = new Database("mongodb://127.0.0.1:27017/test?retryWrites=true&w=majority"); -db.connect(); +const db = require('./db/Database.js'); const serv = http.createServer(app); serv.listen(3500, () => console.log("Serveur lancé !")); \ No newline at end of file diff --git a/views/db/admin.ejs b/views/db/admin.ejs new file mode 100644 index 0000000000000000000000000000000000000000..624c3ecaa5857dfb7a6142d9359b405bdf7564d6 --- /dev/null +++ b/views/db/admin.ejs @@ -0,0 +1,15 @@ +<!DOCTYPE html> +<html lang="fr"> +<head> + <meta charset="UTF-8"> + <title>Admin DB</title> +</head> +<body> + <h1>Admin DB</h1> + <h2>Les collections</h2> + <ul> + <% for (let i=0; i<collections.length; i++){ %> + <li><a href="/db/<%= collections[i].collectionName %>"><%= collections[i].collectionName %></a></li> + <% } %> + </ul> +</html> \ No newline at end of file diff --git a/views/db/collection.ejs b/views/db/collection.ejs new file mode 100644 index 0000000000000000000000000000000000000000..7827ddc82dea4c1fc8ae035c91a0fbd56444a18e --- /dev/null +++ b/views/db/collection.ejs @@ -0,0 +1,26 @@ +<!DOCTYPE html> +<html lang="fr"> +<head> + <meta charset="UTF-8"> + <title>Collection <%= nomCollection %></title> +</head> +<body> + <h1>Collection <%= nomCollection %></h1> + <table> + <tr> + <% if (data.length > 0) { %> + <% Object.keys(data[0]).forEach(key => { %> + <th><%= key %></th> + <% }) %> + <% } %> + </tr> + <% data.forEach(item => { %> + <tr> + <% for(let val of Object.values(item)){ %> + <td><%= val %></td> + <%}%> + </tr> + <% }) %> + </table> +</body> +</html>