Reputation: 13482
In my express app the static assets will not serve on something/something
.
/**
* Express configuration.
*/
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
// app.use(compress());
// app.use(assets({
// paths: ['public/css', 'public/js']
// }));
app.use(logger('dev'));
app.use(favicon(path.join(__dirname, 'public/favicon.png')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator());
app.use(methodOverride());
app.use(cookieParser());
app.use(session({
resave: true,
saveUninitialized: true,
secret: secrets.sessionSecret,
store: new MongoStore({ url: secrets.db, autoReconnect: true })
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(lusca({
csrf: true,
xframe: 'SAMEORIGIN',
xssProtection: true
}));
app.use(function(req, res, next) {
res.locals.user = req.user;
next();
});
app.use(function(req, res, next) {
if (/api/i.test(req.path)) req.session.returnTo = req.path;
next();
});
app.use(express.static(__dirname + '/assets'));
Then in my main layout
<link rel="stylesheet" href="css/main.css">
The home page works, but not the nested routes?
Upvotes: 0
Views: 342
Reputation: 708046
Your stylesheet href is a relative link which means it will be interpreted by the browser to be relative to the path of the current web page, so it will be interpeted differently for a nested path, than a top level web page. You probably need to change your link to be this:
<link rel="stylesheet" href="/css/main.css">
So that it is interpreted the same, no matter what the path of the webpage you put it in happens to be.
Given your express.static()
code, then this path "/css/main.css"
will be looked for as __dirname + "/assets/css/main.css"
.
Upvotes: 5