I am currently creating a website for an artist (my grandfather). My main concern with the code below is readability, as for the most part I have not worked with others on a (programming) project. I use a few external modules but hopefully their purpose is obvious/irrelevant.
I have been programming with javascript as a hobby for about two years.
Here is my code:
const express = require('express')
const app = express()
const fs = require('fs')
const gzip = require('express-gzip')
const handleRequest = require('./handleRequest')
const verify = require('../Admin/login').verify
app.use(gzip)
app.use(express.json())
app.get(['/bio', '/contact', '/login', '/works', '/404', '/bug', '/edit-bio', '/edit-works'], (req, res) => verify(req.query.token).then(result => fs.readFile((req.path === '/edit-bio' || req.path === '/edit-works' ? result : true) ? ((result && req.path === '/login') ? '../../Frontend/html/edit-works.html' : `../../Frontend/html${req.path}.html`) : '../../Frontend/html/login.html', (err, data) => err ? res.status(500).end() : res.end(data))))
app.get('/', (_req, res) => fs.readFile('../../Frontend/index.html', (err, data) => err ? res.status(500).end() : res.end(data)))
app.post(/.+/, (req, res) => handleRequest[req.body.type](req.body).then(result => result.err ? res.status(500).end(JSON.stringify(result.data)) : res.end(JSON.stringify(result.data))))
app.use('/html', (_req, res) => res.status(404).redirect('/404'))
app.use(express.static('../../Frontend'))
app.use((_req, res) => (res.status(404).redirect('/404'))).listen(8080)
This is a simple express server that answers get requests on: '/bio'
, '/contact'
, '/login'
, etc. If the request is to '/edit-bio'
or '/edit-works'
it verifies a jwt provided in the query string. If it receives a post request of any kind it runs a function corresponding to a type
property in that request. It also has a simple 404 page. Finally it uses express.static
on a Frontend
folder containing the css, js, html, etc.
app.get([...])
with a whole bunch of separate paths for the same route and then code inside the route handler to separate out the routes. Make different routes. \$\endgroup\$express.static()
in one middleware configuration for all your static routes rather than manually doingfs.readFile()
andres.end()
for each one separately. \$\endgroup\$express.static()
and then reroute the requests to the paths to the HTML file? And then replaceapp.use('/html', (_req, res) => res.status(404).redirect('/404'))
withapp.use(['/html/edit-bio', '/html/edit-works'], (_req, res) => res.status(404).redirect('/404'))
? \$\endgroup\$app.get(['/bio', '/contact', '/login', '/works', '/404', '/bug', '/edit-bio', '/edit-works'] ...
line of code. Even after putting it into an editor and breaking it into multiple lines, it still isn't obvious to me what that line does except that it's got multiple checks for various paths which means you aren't taking advantage of the simplicity of using separate route declarations for separate paths. And, I can't imagine how one would add three more routes to it without a pretty good change of breaking something. \$\endgroup\$express.static()
. But, the MAIN point is that that line is not easily readable. Probably several different approaches to simplify it. \$\endgroup\$