From e92c87c5da54a5b9211601262f39b860215fd3a8 Mon Sep 17 00:00:00 2001 From: biglyderv Date: Mon, 25 Nov 2024 14:12:43 -0500 Subject: [PATCH] change to ejs --- client/login.js | 12 +++++++----- client/main.js | 18 +++--------------- client/register.js | 14 ++++++++------ common/format.js | 18 ------------------ comp/form.js | 15 --------------- index.js | 20 +++++--------------- package.json | 1 + views/footer.ejs | 2 ++ views/form.ejs | 11 +++++++++++ views/header.ejs | 9 +++++++++ views/main.ejs | 16 ++++++++++++++++ 11 files changed, 62 insertions(+), 74 deletions(-) delete mode 100644 common/format.js delete mode 100644 comp/form.js create mode 100644 views/footer.ejs create mode 100644 views/form.ejs create mode 100644 views/header.ejs create mode 100644 views/main.ejs diff --git a/client/login.js b/client/login.js index 2d30e8d..8213f36 100644 --- a/client/login.js +++ b/client/login.js @@ -1,12 +1,14 @@ import Route from "../route.js"; -import form from "../comp/form.js"; // TODO: rewrite let main = new Route([], async function (req, res, input) { - return form([ - { label: "Username", type: "text", name: "user" }, - { label: "Password", type: "password", name: "pass" } - ],'/api/form/register') + return res.render('form', { + data: [ + { label: "Username", type: "text", name: "user" }, + { label: "Password", type: "password", name: "pass" } + ], + 'route': '/api/form/login' + }); }); export default main; \ No newline at end of file diff --git a/client/main.js b/client/main.js index 105c7fe..bef7414 100644 --- a/client/main.js +++ b/client/main.js @@ -1,23 +1,11 @@ import Route from "../route.js"; import auth from "../form/auth.js"; -import format from "../common/format.js"; let main = new Route([auth], async function (req, res, input) { let { username } = input; - let cta = (username == '!nobody') ? - '
Join
' : - '
Chat
'; - - return `
-

About

-

- BiglyChat is a chat service built on free software. -

-

- Anyone can selfhost an instance to take control of their community. -

- ${cta} -
` + return res.render('main', { + username + }); }); export default main; \ No newline at end of file diff --git a/client/register.js b/client/register.js index 9ec57c0..656d207 100644 --- a/client/register.js +++ b/client/register.js @@ -1,13 +1,15 @@ import Route from "../route.js"; -import form from "../comp/form.js"; // TODO: rewrite let main = new Route([], async function (req, res, input) { - return form([ - { label: "Username", type: "text", name: "user" }, - { label: "Password", type: "password", name: "pass" }, - { label: "Password (again)", type: "password", name: "pass2" }, - ],'/api/form/register') + return res.render('form', { + data: [ + { label: "Username", type: "text", name: "user" }, + { label: "Password", type: "password", name: "pass" }, + { label: "Password (again)", type: "password", name: "pass2" } + ], + 'route': '/api/form/register' + }); }); export default main; \ No newline at end of file diff --git a/common/format.js b/common/format.js deleted file mode 100644 index 7d27dfa..0000000 --- a/common/format.js +++ /dev/null @@ -1,18 +0,0 @@ -const escapeCodes = { - '&': '&', - '<': '<', - '>': '>', - "'": ''', - '"': '"' -}; - -let format = {}; - -format.escape = function (str) { - return str.replaceAll( - /[&<>'"]/g, - tag => escapeCodes[tag] - ) -}; - -export default format; \ No newline at end of file diff --git a/comp/form.js b/comp/form.js deleted file mode 100644 index 3457f69..0000000 --- a/comp/form.js +++ /dev/null @@ -1,15 +0,0 @@ -function form(elems, action) { - elems.push({ - 'label': 'Submit', - 'type': 'Submit', - 'name': 'submit' - }); - - let mapped = elems.map(elem => - ` ${elem.label} ` - ).join('\n'); - - return `
${mapped}
` -} - -export default form; \ No newline at end of file diff --git a/index.js b/index.js index eece498..c3b9394 100644 --- a/index.js +++ b/index.js @@ -2,28 +2,18 @@ import express from 'express'; import multer from 'multer'; import iterate from './routes.js'; import cookieParser from 'cookie-parser'; +import ejs from "ejs"; const port = process.env.PORT || 3000; -const header = ` - - - - - - -` - -const footer = ` - - -`; - const app = express(); const upload = multer({ dest: 'uploads/' }); app.use(cookieParser()); app.use('/static', express.static('static')); +app.engine('.ejs', ejs.__express); +app.set('views', './views'); +app.set('view engine', 'ejs'); app.get('/', (req, res) => { res.status(301).redirect('/client/main'); @@ -31,7 +21,7 @@ app.get('/', (req, res) => { app.get('/client/:route', async (req, res) => { let dat = await iterate(req, res, 'client'); - res.send(header + dat + footer); + //res.send(dat); }) app.get('/api/get/:route', async (req, res) => { diff --git a/package.json b/package.json index 056a997..781f202 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "dependencies": { "bcrypt": "^5.1.1", "cookie-parser": "^1.4.6", + "ejs": "^3.1.10", "express": "^4.19.2", "sqlite": "^5.1.1", "sqlite3": "^5.1.7", diff --git a/views/footer.ejs b/views/footer.ejs new file mode 100644 index 0000000..691287b --- /dev/null +++ b/views/footer.ejs @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/views/form.ejs b/views/form.ejs new file mode 100644 index 0000000..9d033b0 --- /dev/null +++ b/views/form.ejs @@ -0,0 +1,11 @@ +<%- include('header.ejs') -%> +
+
+ <% for (let elem of data) { %> + + <%= elem.label %> + + <% } %> + +
+ <%- include('footer.ejs') -%> \ No newline at end of file diff --git a/views/header.ejs b/views/header.ejs new file mode 100644 index 0000000..668896b --- /dev/null +++ b/views/header.ejs @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/views/main.ejs b/views/main.ejs new file mode 100644 index 0000000..469fc9a --- /dev/null +++ b/views/main.ejs @@ -0,0 +1,16 @@ +<%- include('header.ejs') -%> +
+

About

+

+ BiglyChat is a project sharing communtiy built on Scratch and + TurboWarp + . The source code is free software. +

+

+ Anyone can selfhost an instance to take control of their community. +

+ <%if (username=='!nobody' ) { %> + + <% } %> +
+ <%- include('footer.ejs') -%> \ No newline at end of file