The start point of my React app is:
localhost:3000/Admin
So when i am typing in the url this:
localhost:3000/admin/dashboard/
It works all right. But when I write the next nested url:
localhost:3000/admin/dashboard/new
the index.html doesn’t load my script and css. When I change in the index.html the src url from ./js/admin-app.js to ../../js/admin-app.js it works but not more on the other urls.
Localhost:3000/admin/dashboard
I have tried to set in the index.html base href="./" but it doesn't work.
I can’t find the problem .
my server.js:
app.use('/admin', express.static(path.join(__dirname, 'admin/server/static/')));
app.use('/', express.static(path.join(__dirname, 'server/static/')));
app.get('/admin/*', function(req, res) {
res.sendFile(path.join(__dirname, 'admin/server/static/index.html'), function(err) {
if (err) {
res.status(500).send(err)
}
})
});
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, 'server/static/index.html'), function(err) {
if (err) {
res.status(500).send(err)
}
})
});
Index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<base href="./" />
<title>AdminPage</title>
<link rel="stylesheet" href="css/admin_style.css">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<div id="admin-app"></div>
<script src="js/admin_app.js"></script>
</body>
</html>
Solution
My App runs at
Localhost:3000/admin/
Instead of
src="js/admin_app.js" or src="./js/admin_app.js"
i try this
src="/admin/js/admin_app.js"
and it works perfectly
edityour question and add the code there. Don't add it as an answer. Please move that code to your question, and delete your answer.