express does not include assets - node.js

I'm trying to load assets to my EJS file using express and it does not work. I just get message
Cannot GET /assets/main.css
Even I made loader in my main app:
app.use(express.static(path.join(__dirname, './assets')));
and after printing the path I see that its correct and the files exsists. So after using
<link href="assets/main.css" rel="stylesheet"> I can't reach the file. Where the problem could be?

See Serving static files in Express.
Right now, your app.use(...) statement is saying: I want the directory ./assets to be served whenever I navigate to my app, in other words, when I open my browser to http://localhost, serve whatever is in the folder ./assets.
You are then trying to access the file main.css at http://localhost/assets/main.css. It isn't there, it's at http://localhost/main.css.
You have 2 options:
Change your <link> tag to point to where the asset actually is:
<link href="main.css" rel="stylesheet">
Change your app.use() to host the ./assets folder at a different endpoint:
app.use('/assets', express.static(path.join(__dirname, './assets')));

Related

Cannot access static files through certain routes in nodejs

My routes are configured in app.js. The /users route works as expected except when rendering express layouts I cannot access the css files located at ./css/style.css
If I open a page using the /users route, I can see it's looking for /users/css/style.css but I thought setting the directory to static would override this?
app.use('/', require('./routes/index'));
app.use('/users', require('./routes/users'));
app.use(express.static('css'));
First of all, what app.use(express.static('css')); does is to serve statically the files starting from the given path, in this case css.
A good practice is to create a folder called public and use it to serve it's inner files statically. So your static folder should be like this:
-public
-css
-style.css
and your app.js should have app.use(express.static('./public'));
Now, another problem you may be having is the way you cast the paths (url) to load certain files.
Let's say you request http://localhost:PORT/users and the served HTML loads a stylesheet using <link rel="stylesheet" type="text/css" href="PATH">.
You could write the PATH in 2 ways.
css/style.css
/css/style.css
The difference is that the first method will search for the file relative to the path you're already in (e.g http://localhost:PORT/users/css/style.css). The other method will get such file using the 'domain' you are already in as a starting point (e.g http://localhost:PORT/css/style.css.
Hope this helps c:
I figured it out. Added this to my app.js file:
// GET /static/style.css etc.
app.use('/users', express.static(path.join(__dirname, 'public')))
More information here if needed: http://expressjs.com/en/api.html

node server is not serving static files

I have created a nodejs application using the express framework. I have used express.static to serve my static files.
app.use(express.static(path.join(__dirname, '..', 'public')));
There is a route that server the login.html.
// serve login page
app.get('/auth/login', loginPage);
and loginPage function
loginPage(req: any, res: any) {
res.sendFile(path.join(__dirname, '..', '..', 'public', 'login.html'));
}
Now if I directly hit http://localhost:8080/login.html login page gets served with all its css and js files, so static files are getting served.
but when I hit http://localhost:8080/auth/login only login.html is served and the other css/js files showing 404 status.
If I check the request on browser network window request is for http://localhost:8080/auth/css/bootstrap.min.css, this is appending auth prefix.
but when I change URL mapping to /auth from /auth/login then it works fine.
How can I solve this?
I was linking other js and CSS files without using / in href attribute. Like this
<link href="css/bootstrap.min.css" rel="stylesheet">
So I changed all the href by appending the /
<link href="/css/bootstrap.min.css" rel="stylesheet">
and now this is working. Thanks to #Patric

Static files(css,js) are not loading, node express angular project

I have a project which has been built using Node js, Express framework and Angular js. I have deployed the project on Bluemix. When I hit the URL in browser the static content(css, js) is not being loaded. Browser says 404 Not found.
Folder structure: Views-->index.ejs
!
!
Content--->style.css
Views and Content folders are at the same level.
I tried below paths.
<link href="Content/style.css" rel="stylesheet">
<link href="/Content/style.css" rel="stylesheet">
<link href="../Content/style.css" rel="stylesheet">
Thanks in advance
You need to make sure you set the public directory for your files to be accessed by express. This is an example:
app.use(express.static(__dirname + '/public'));
Then you need to reference your front-end files (css, img, etc.) in reference to this directory (so if you have a public/img folder, the reference url will be <img src="img/NAMEOFIMAGE.jpg">
Hope this helps. Comment below with any issues.
Thanks

Node.js Express - Specifying path to root of app

I am trying to access a folder in my app that contains a few .js controller modules.
in the head of one of my .html files I have this script declared:
the problem is when the app is running, the application seems to know what the current path is, so the path to my script is not relative to the actual file system, it is relative to the current path of the app, which I might say is very strange.
So, the quick fix would be to easily specify the root of my application, since fancy_scripts is just one folder down from the root.
the following doesn't work, because of the problem I mentioned above
<head>
<script type="text/javascript" src="../fancy_scripts/userHomeController.js"></script>
</head>
but there has to be a way to specify the global root of the app with something like a double slash "//"
<head>
<script type="text/javascript" src="//fancy_scripts/userHomeController.js"></script>
</head>
(the above doesn't work either, // is the root in MS Windows (I think), but not an Express app) this is a straightforward problem, with I hope a straightforward solution. There might be a better way to do things however.
I have these 3 lines of code already:
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(__dirname + '/fancy_scripts'));
app.use('/fancy_scripts', express.static(__dirname + '/fancy_scripts'));
When using express, the path will be routed through one of your declared handlers or via the static module if you've set that up. Normally Express is configured to use static and to map it to your applications public folder. When using the express-generator that folder is located at <approot>/public/. So in order to access something like this:
<head>
<script type="text/javascript" src="/fancy_scripts/userHomeController.js"></script>
</head>
You'll need to put userHomeController in <approot>/public/fancy_scripts/userHomeController.js

node.js - can't load my files with express3

I am using express3 and when I'm trying to load my html/javascript/css files it loads the html without the css.
I'm using this code to load the files -
express.get('/', function (req, res) {
res.sendfile("index.html");
res.sendfile("style.css");
res.sendfile("script.js");
});
So what can I do?
First, link the css and javascript to the page via tags in the header of your HTML
<link rel='stylesheet' href='/path/to/style.css' />
<script src='/path/to/javascript.js'></script>
Be sure that you're using the static middleware in your app.use() section, this will tell Express where to find your static files.
app.use(express.static(__dirname + '/public'));
Now, your Express app should serve your static css and js files.
You link to css and javascript from the html page...
Try looking at the static files example app in the express github repo: https://github.com/visionmedia/express/tree/master/examples/static-files
Basically you make another http request for static files (.js, .css)

Resources