Skip to content
Snippets Groups Projects
Commit da7689cc authored by VILLAUME Lucas's avatar VILLAUME Lucas
Browse files

Interface web administrer la DB

parent d29e91b8
No related branches found
No related tags found
No related merge requests found
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
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
<!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
<!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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment