From bd84e31b3057379333cb1f158a4bc599e36ad9d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Galv=C3=A3o?= Date: Mon, 11 Aug 2025 11:28:25 -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 6c8d788..af4a79a 100644 --- a/server.js +++ b/server.js @@ -3,21 +3,21 @@ const app = express(); const path = require('path'); const PORT = process.env.PORT || 3500; -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) => { @@ -41,10 +41,10 @@ const three = (req, res) => { res.send('Finished!'); } -app.get('/chain(.html)?', [one, two, three]); +app.get('/chain{.html}', [one, two, three]); -app.get('/*', (req, res) => { +app.get('*name', (req, res) => { res.status(404).sendFile(path.join(__dirname, 'views', '404.html')); }) -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}`));