From 6c43050a8c7d58ce35d4b595ab8ed346e480c950 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sat, 31 Aug 2019 00:39:51 +0200 Subject: [PATCH] Add source code link, basic input validation, and monospace error display --- index.js | 14 +++++++++++++- package.json | 3 ++- public/style.css | 5 +++++ views/error.jsx | 4 +++- views/layout.jsx | 1 + 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 592dc1c..407df69 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ const url = require("url"); const autodiscoverClientConfiguration = require("@modular-matrix/autodiscover-client-configuration"); const bhttp = require("bhttp"); const createError = require("create-error"); +const { ValidationError, validateValue, required, isString } = require("validatem"); let UpstreamError = createError("UpstreamError"); @@ -34,6 +35,17 @@ router.get("/", (req, res) => { }); router.get("/show-rooms", (req, res) => { + validateValue(req.query, { + hostname: [ required, isString, (string) => { + console.log(string.length); + + if (string.length === 0) { + throw new ValidationError("May not be empty"); + } + } ], + since: [ isString ] + }); + return Promise.try(() => { return autodiscoverClientConfiguration.discover(req.query.hostname); }).then((clientConfiguration) => { @@ -68,7 +80,7 @@ router.get("/show-rooms", (req, res) => { app.use(router); app.use((error, req, res, next) => { - if (error instanceof UpstreamError || error instanceof autodiscoverClientConfiguration.LookupFailed) { + if (error instanceof UpstreamError || error instanceof autodiscoverClientConfiguration.LookupFailed || error instanceof ValidationError) { res.render("error", { error: error }); } else { throw error; diff --git a/package.json b/package.json index cdb64d0..90fba8d 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "express": "^4.17.1", "express-promise-router": "^3.0.3", "react": "^16.9.0", - "react-dom": "^16.9.0" + "react-dom": "^16.9.0", + "validatem": "^0.2.0" }, "devDependencies": { "nodemon": "^1.19.1" diff --git a/public/style.css b/public/style.css index 22391c4..670451a 100644 --- a/public/style.css +++ b/public/style.css @@ -2,6 +2,11 @@ body { font-family: sans-serif; } +h1 { + display: inline-block; + margin-right: 2em; +} + .container { max-width: 960px; margin: 0 auto; diff --git a/views/error.jsx b/views/error.jsx index f39ede4..beab73f 100644 --- a/views/error.jsx +++ b/views/error.jsx @@ -7,7 +7,9 @@ const Layout = require("./layout"); module.exports = function Error({ error }) { return ( - {error.message} +
+				{error.message}
+			
); }; diff --git a/views/layout.jsx b/views/layout.jsx index 3c747a0..3dfb30a 100644 --- a/views/layout.jsx +++ b/views/layout.jsx @@ -15,6 +15,7 @@ module.exports = function Layout({ children }) {

Matrix Room List Viewer

+ Source code
{children}