Skip to content

Instantly share code, notes, and snippets.

@szkrd
Created September 20, 2016 15:20
Show Gist options
  • Save szkrd/140fc0029b059b8da369ba3d9d131e7e to your computer and use it in GitHub Desktop.
Save szkrd/140fc0029b059b8da369ba3d9d131e7e to your computer and use it in GitHub Desktop.
Testing webpack devserver rewrite rules is a pain in the back.
/*
{
"name": "mod-rewrite-test",
"version": "0.0.1",
"main": "index.js",
"scripts": { "dev": "nodemon .", "start": "node ." },
"dependencies": { "connect-modrewrite": "^0.9.0", "express": "^4.14.0" },
"devDependencies": { "nodemon": "^1.9.2" }
}
*/
const modRewrite = require('connect-modrewrite')
const app = require('express')()
const http = require('http').Server(app)
const locales = 'hu-hu|bg-bg|cs-cz|bs-latn-ba|de-de|en-gb|es-es|fr-fr|he-il|it-it|lv-lv|mk-mk|lt-lt|nl-nl|nb-no|pl-pl|pt-pt|ro-ro|ru-ru|sk-sk|sv-se|sr-cyrl-cs|uk-ua|ka-ge'
const rtlLocales = 'he-il'
let pages = 'itinerary|profile|baggage-fee-calculator|wdc-acceptance'
pages += '|' + pages.replace(/\|/g, '_rtl|') + '_rtl'
app.use(modRewrite([
'^/$ /index.html [L]', // "/"
'^/rtl/?$ /index_rtl.html [L]', // "/rtl" "/rtl/"
`^/(${pages})/?$ /$1.html [L]`, // "/profile" "/profile_rtl" "/itinerary" "/itinerary_rtl"
`^/(${rtlLocales})/(${pages})/?$ /$2_rtl.html [L]`, // not perfect, may work for most cases
`^/(${locales})/(${pages})/?$ /$2.html [L]`, // "/hu-hu/profile" "/he-il/profile_rtl"
`^/(${locales})/rtl/?$ /index_rtl.html [L]`, // "/he-il/rtl/"
`^/(${rtlLocales})/?$ /index_rtl.html [L]`, // not perfect, may work for most cases
`^/(${locales})/?$ /index.html [L]` // "/he-il/"
]))
//-----------------------------
app.get('/test', (req, res) => {
const urls = [
'/', '/rtl', '/rtl/', '/profile', '/itinerary', '/hu-hu', '/hu-hu/', '/he-il/', '/hu-hu/profile',
'/he-il/itinerary', '/hu-hu/rtl/', '/hu-hu/rtl', '/hu-hu/profile_rtl', '/hu-hu/itinerary_rtl'
]
const lis = urls.map(url => `<li><a target="test" href="${url}">${url}</a></li>`).join('')
res.send(`<ul>${lis}</ul><iframe name="test"></iframe>`)
});
app.get(/.*/, (req, res) => { res.send(req.path); })
//-----------------------------
http.listen(4100, function(){ console.log('listening on 4100'); })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment