I have encountered an issue with applying CSS and JS while routing in my nodejs project. I have tested the following lines:
app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname, '/public'));
app.use('/static', express.static(__dirname, '/public));
Am I missing something? The client console shows this message: Refused to apply style from 'http://localhost:3000/users/css/styles.css' However, my CSS file is not located in the 'users' folder. It is actually placed in a folder named 'public' in the same directory as my 'server.js' file.
const express = require('express');
const port = 3000;
const app = express();
app.set('view engine', 'pug');
app.use(express.static('public'));
app.get('/', (req, res) => {
res.render('pages/home');
});
// This code functions properly, but I wish to implement different routing
app.get('/register', (req, res) => {
res.render('pages/register');
});
// While this code successfully renders the page, the CSS and JS files do not apply
app.get('/users/register', (req, res) => {
res.render('pages/register');
});
app.listen(port, () => {
console.log('Server is currently running on port ' + port);
});