node.js won't load css styles in all pages
I'm writing a simple blog from scratch to learn node.js + express. I'm stumped on an issue though, when I try to access a directory that's nested, the styles won't load. For example:
app.get('/posts/new', (req, res) => {
res.render('create')
});
will not use the styles, but simply using '/posts' will.
any idea what's causing this? Here's the full code:
const path = require('path');
const expressEdge = require('express-edge');
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = new express();
mongoose.connect('mongodb://localhost:27017/node-blog', {
useNewUrlParser: true
})
.then(() => 'You are now connected to Mongo!')
.catch(err => console.error('Something went wrong', err))
app.use(express.static('public'));
app.use(expressEdge);
app.set('views', __dirname + '/views');
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/', (req, res) => {
res.render('index');
});
app.get('/posts/new', (req, res) => {
res.render('create')
});
app.post('/posts/store', (req, res) => {
console.log(req.body)
res.redirect('/')
});
app.listen(4000)
And here is directory structure:
└── node-blog
├── database
├── node_modules
├── public
├── css
├── img
├── vendor
├── js
├── theme
└── views
├── layouts
all the relevant styles are in public and the templating engine files are located in views.
css node.js express
add a comment |
I'm writing a simple blog from scratch to learn node.js + express. I'm stumped on an issue though, when I try to access a directory that's nested, the styles won't load. For example:
app.get('/posts/new', (req, res) => {
res.render('create')
});
will not use the styles, but simply using '/posts' will.
any idea what's causing this? Here's the full code:
const path = require('path');
const expressEdge = require('express-edge');
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = new express();
mongoose.connect('mongodb://localhost:27017/node-blog', {
useNewUrlParser: true
})
.then(() => 'You are now connected to Mongo!')
.catch(err => console.error('Something went wrong', err))
app.use(express.static('public'));
app.use(expressEdge);
app.set('views', __dirname + '/views');
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/', (req, res) => {
res.render('index');
});
app.get('/posts/new', (req, res) => {
res.render('create')
});
app.post('/posts/store', (req, res) => {
console.log(req.body)
res.redirect('/')
});
app.listen(4000)
And here is directory structure:
└── node-blog
├── database
├── node_modules
├── public
├── css
├── img
├── vendor
├── js
├── theme
└── views
├── layouts
all the relevant styles are in public and the templating engine files are located in views.
css node.js express
Thanks for the question! A total shot in the dark is this: It is possible your path to include CSS is relative rather than absolute. For example,<link href="styles/base.css" rel="stylesheet" type="text/css" media="all">and as a result, the path is off when in a subdirectory. You could try,<link href="/styles/base.css" rel="stylesheet" type="text/css" media="all">.
– Harvey A. Ramer
Nov 17 '18 at 3:31
Please show us a specific<link>tag from your page that does not work and, if it's not an absolute link, then also include the URL of the page its in.
– jfriend00
Nov 17 '18 at 4:05
It worked! I was using a relative path, not an absolute one. Thanks!
– wont
Nov 17 '18 at 14:10
add a comment |
I'm writing a simple blog from scratch to learn node.js + express. I'm stumped on an issue though, when I try to access a directory that's nested, the styles won't load. For example:
app.get('/posts/new', (req, res) => {
res.render('create')
});
will not use the styles, but simply using '/posts' will.
any idea what's causing this? Here's the full code:
const path = require('path');
const expressEdge = require('express-edge');
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = new express();
mongoose.connect('mongodb://localhost:27017/node-blog', {
useNewUrlParser: true
})
.then(() => 'You are now connected to Mongo!')
.catch(err => console.error('Something went wrong', err))
app.use(express.static('public'));
app.use(expressEdge);
app.set('views', __dirname + '/views');
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/', (req, res) => {
res.render('index');
});
app.get('/posts/new', (req, res) => {
res.render('create')
});
app.post('/posts/store', (req, res) => {
console.log(req.body)
res.redirect('/')
});
app.listen(4000)
And here is directory structure:
└── node-blog
├── database
├── node_modules
├── public
├── css
├── img
├── vendor
├── js
├── theme
└── views
├── layouts
all the relevant styles are in public and the templating engine files are located in views.
css node.js express
I'm writing a simple blog from scratch to learn node.js + express. I'm stumped on an issue though, when I try to access a directory that's nested, the styles won't load. For example:
app.get('/posts/new', (req, res) => {
res.render('create')
});
will not use the styles, but simply using '/posts' will.
any idea what's causing this? Here's the full code:
const path = require('path');
const expressEdge = require('express-edge');
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = new express();
mongoose.connect('mongodb://localhost:27017/node-blog', {
useNewUrlParser: true
})
.then(() => 'You are now connected to Mongo!')
.catch(err => console.error('Something went wrong', err))
app.use(express.static('public'));
app.use(expressEdge);
app.set('views', __dirname + '/views');
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/', (req, res) => {
res.render('index');
});
app.get('/posts/new', (req, res) => {
res.render('create')
});
app.post('/posts/store', (req, res) => {
console.log(req.body)
res.redirect('/')
});
app.listen(4000)
And here is directory structure:
└── node-blog
├── database
├── node_modules
├── public
├── css
├── img
├── vendor
├── js
├── theme
└── views
├── layouts
all the relevant styles are in public and the templating engine files are located in views.
css node.js express
css node.js express
asked Nov 17 '18 at 3:24
wontwont
82
82
Thanks for the question! A total shot in the dark is this: It is possible your path to include CSS is relative rather than absolute. For example,<link href="styles/base.css" rel="stylesheet" type="text/css" media="all">and as a result, the path is off when in a subdirectory. You could try,<link href="/styles/base.css" rel="stylesheet" type="text/css" media="all">.
– Harvey A. Ramer
Nov 17 '18 at 3:31
Please show us a specific<link>tag from your page that does not work and, if it's not an absolute link, then also include the URL of the page its in.
– jfriend00
Nov 17 '18 at 4:05
It worked! I was using a relative path, not an absolute one. Thanks!
– wont
Nov 17 '18 at 14:10
add a comment |
Thanks for the question! A total shot in the dark is this: It is possible your path to include CSS is relative rather than absolute. For example,<link href="styles/base.css" rel="stylesheet" type="text/css" media="all">and as a result, the path is off when in a subdirectory. You could try,<link href="/styles/base.css" rel="stylesheet" type="text/css" media="all">.
– Harvey A. Ramer
Nov 17 '18 at 3:31
Please show us a specific<link>tag from your page that does not work and, if it's not an absolute link, then also include the URL of the page its in.
– jfriend00
Nov 17 '18 at 4:05
It worked! I was using a relative path, not an absolute one. Thanks!
– wont
Nov 17 '18 at 14:10
Thanks for the question! A total shot in the dark is this: It is possible your path to include CSS is relative rather than absolute. For example,
<link href="styles/base.css" rel="stylesheet" type="text/css" media="all"> and as a result, the path is off when in a subdirectory. You could try, <link href="/styles/base.css" rel="stylesheet" type="text/css" media="all">.– Harvey A. Ramer
Nov 17 '18 at 3:31
Thanks for the question! A total shot in the dark is this: It is possible your path to include CSS is relative rather than absolute. For example,
<link href="styles/base.css" rel="stylesheet" type="text/css" media="all"> and as a result, the path is off when in a subdirectory. You could try, <link href="/styles/base.css" rel="stylesheet" type="text/css" media="all">.– Harvey A. Ramer
Nov 17 '18 at 3:31
Please show us a specific
<link> tag from your page that does not work and, if it's not an absolute link, then also include the URL of the page its in.– jfriend00
Nov 17 '18 at 4:05
Please show us a specific
<link> tag from your page that does not work and, if it's not an absolute link, then also include the URL of the page its in.– jfriend00
Nov 17 '18 at 4:05
It worked! I was using a relative path, not an absolute one. Thanks!
– wont
Nov 17 '18 at 14:10
It worked! I was using a relative path, not an absolute one. Thanks!
– wont
Nov 17 '18 at 14:10
add a comment |
1 Answer
1
active
oldest
votes
you can load the static assets by creating the virtual path likeapp.use('/assets',express.static(__dirname + '/public/css'));
where public is the directory where all your assets are stored,in which css is the folder where is all your css file are stored , you can you the virtual path in the link tag , href attribute for loading the css ,eg: if you have template file ,you write in it ,the link tag
<link rel="stylesheet" type="text/css" href="/assets/create.css"> i have tried with the same directory structure like yours and tried to emulate the bug and fixed the css load issue you can refer https://github.com/ardnahcivar/Node.js-Code-/tree/master/11-17-18 the code
Thank you, this also worked. Changing the pathing in the layout file to <link href="../css/clean-blog.css" rel="stylesheet"> also made it work.
– wont
Nov 17 '18 at 14:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347895%2fnode-js-wont-load-css-styles-in-all-pages%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
you can load the static assets by creating the virtual path likeapp.use('/assets',express.static(__dirname + '/public/css'));
where public is the directory where all your assets are stored,in which css is the folder where is all your css file are stored , you can you the virtual path in the link tag , href attribute for loading the css ,eg: if you have template file ,you write in it ,the link tag
<link rel="stylesheet" type="text/css" href="/assets/create.css"> i have tried with the same directory structure like yours and tried to emulate the bug and fixed the css load issue you can refer https://github.com/ardnahcivar/Node.js-Code-/tree/master/11-17-18 the code
Thank you, this also worked. Changing the pathing in the layout file to <link href="../css/clean-blog.css" rel="stylesheet"> also made it work.
– wont
Nov 17 '18 at 14:12
add a comment |
you can load the static assets by creating the virtual path likeapp.use('/assets',express.static(__dirname + '/public/css'));
where public is the directory where all your assets are stored,in which css is the folder where is all your css file are stored , you can you the virtual path in the link tag , href attribute for loading the css ,eg: if you have template file ,you write in it ,the link tag
<link rel="stylesheet" type="text/css" href="/assets/create.css"> i have tried with the same directory structure like yours and tried to emulate the bug and fixed the css load issue you can refer https://github.com/ardnahcivar/Node.js-Code-/tree/master/11-17-18 the code
Thank you, this also worked. Changing the pathing in the layout file to <link href="../css/clean-blog.css" rel="stylesheet"> also made it work.
– wont
Nov 17 '18 at 14:12
add a comment |
you can load the static assets by creating the virtual path likeapp.use('/assets',express.static(__dirname + '/public/css'));
where public is the directory where all your assets are stored,in which css is the folder where is all your css file are stored , you can you the virtual path in the link tag , href attribute for loading the css ,eg: if you have template file ,you write in it ,the link tag
<link rel="stylesheet" type="text/css" href="/assets/create.css"> i have tried with the same directory structure like yours and tried to emulate the bug and fixed the css load issue you can refer https://github.com/ardnahcivar/Node.js-Code-/tree/master/11-17-18 the code
you can load the static assets by creating the virtual path likeapp.use('/assets',express.static(__dirname + '/public/css'));
where public is the directory where all your assets are stored,in which css is the folder where is all your css file are stored , you can you the virtual path in the link tag , href attribute for loading the css ,eg: if you have template file ,you write in it ,the link tag
<link rel="stylesheet" type="text/css" href="/assets/create.css"> i have tried with the same directory structure like yours and tried to emulate the bug and fixed the css load issue you can refer https://github.com/ardnahcivar/Node.js-Code-/tree/master/11-17-18 the code
answered Nov 17 '18 at 6:18
raviravi
25027
25027
Thank you, this also worked. Changing the pathing in the layout file to <link href="../css/clean-blog.css" rel="stylesheet"> also made it work.
– wont
Nov 17 '18 at 14:12
add a comment |
Thank you, this also worked. Changing the pathing in the layout file to <link href="../css/clean-blog.css" rel="stylesheet"> also made it work.
– wont
Nov 17 '18 at 14:12
Thank you, this also worked. Changing the pathing in the layout file to <link href="../css/clean-blog.css" rel="stylesheet"> also made it work.
– wont
Nov 17 '18 at 14:12
Thank you, this also worked. Changing the pathing in the layout file to <link href="../css/clean-blog.css" rel="stylesheet"> also made it work.
– wont
Nov 17 '18 at 14:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347895%2fnode-js-wont-load-css-styles-in-all-pages%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Thanks for the question! A total shot in the dark is this: It is possible your path to include CSS is relative rather than absolute. For example,
<link href="styles/base.css" rel="stylesheet" type="text/css" media="all">and as a result, the path is off when in a subdirectory. You could try,<link href="/styles/base.css" rel="stylesheet" type="text/css" media="all">.– Harvey A. Ramer
Nov 17 '18 at 3:31
Please show us a specific
<link>tag from your page that does not work and, if it's not an absolute link, then also include the URL of the page its in.– jfriend00
Nov 17 '18 at 4:05
It worked! I was using a relative path, not an absolute one. Thanks!
– wont
Nov 17 '18 at 14:10