From 122073df370655aab4571229cf43886a809cd7a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Galv=C3=A3o?= Date: Mon, 11 Aug 2025 11:33:31 -0300 Subject: [PATCH] Fix regular expressions in server.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Express v5 release update no longer supports “sub-expression” regular expressions. This release includes simplified patterns for common route patterns. With the removal of regular expression semantics comes other small but impactful changes to how you write your routes. 1. :name? becomes {:name}. Usage of {} for optional parts of your route means you can now do things like /base{/:optional}/:required and what parts are actually optional is much more explicit. 2. '*' becomes '*name'. Oficial documentation: https://expressjs.com/2024/10/15/v5-release.html --- server.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/server.js b/server.js index 5f8859e..e473f73 100644 --- a/server.js +++ b/server.js @@ -34,21 +34,21 @@ app.use(express.json()); //serve static files app.use(express.static(path.join(__dirname, '/public'))); -app.get('^/$|/index(.html)?', (req, res) => { +app.get(['/', '/index{.html}'], (req, res) => { //res.sendFile('./views/index.html', { root: __dirname }); res.sendFile(path.join(__dirname, 'views', 'index.html')); }); -app.get('/new-page(.html)?', (req, res) => { +app.get('/new-page{.html}', (req, res) => { res.sendFile(path.join(__dirname, 'views', 'new-page.html')); }); -app.get('/old-page(.html)?', (req, res) => { +app.get('/old-page{.html}', (req, res) => { res.redirect(301, '/new-page.html'); //302 by default }); // Route handlers -app.get('/hello(.html)?', (req, res, next) => { +app.get('/hello{.html}', (req, res, next) => { console.log('attempted to load hello.html'); next() }, (req, res) => { @@ -72,9 +72,9 @@ const three = (req, res) => { res.send('Finished!'); } -app.get('/chain(.html)?', [one, two, three]); +app.get('/chain{.html}', [one, two, three]); -app.all('*', (req, res) => { +app.all('*name', (req, res) => { res.status(404); if (req.accepts('html')) { res.sendFile(path.join(__dirname, 'views', '404.html')); @@ -87,4 +87,4 @@ app.all('*', (req, res) => { app.use(errorHandler); -app.listen(PORT, () => console.log(`Server running on port ${PORT}`)); \ No newline at end of file +app.listen(PORT, () => console.log(`Server running on port ${PORT}`));