static middle ware in express just doesn't serve files - node.js

i have the following directory structure
/app
js
css
/node
server.js
index.html
dashboard.html
I am using
app.use(express.static( __dirname + '/../'));
for accessing index.html and dashobard.html (in server.js)
The following route works just fine:
app.get('/',function(req,res){
res.sendfile('index.html');
});
But this one doesn't work
app.get('/dashboard',ensureAuthenticated,function(req,res){
res.sendfile('dashboard.html');
});
I get this
Error: ENOENT, stat 'dashboard.html'

You can call res.sendfile without express.static
For your problem:
I guess both res.sendfile('index.html') and res.sendfile('dashboard.html') are not correct, they should be
var root = fs.realpathSync('..');
res.sendfile(path.join(root, 'index.html'))
res.sendfile(path.join(root, 'dashboard.html'));
But why you do not see bug on app.get('/', ...)?
because it is already handle by static middleware. Note / <==> /index.html.
About err on /dashboard
app.get('/dashboard', ...) is called and it can not find 'dashboard.html' in '/node' folder so the err ENOENT happened.

Related

Why I'm getting a blank page from running a node server that point to the /dist folder of an Angular app?

My simple node server is:
const express = require('express');
const app = express();
app.get('*', (req, res) => {
res.sendFile(__dirname + '/dist/page/index.html');
})
app.listen(3335, () => { console.log('Server is listening on 3335'); });
But I'm getting the index file that seems not running the main.js
The Angular app, at the moment it's literally a page/component that is app.component.ts, so there is not any routing.
Use express.static() for serving static files.
It is because you have set a response of 'index.html' file for each and every request the server would receive. The first response would be good that's the index.html page only as expected. But, the index.html page must be having some script and css tags to fetch your Angular Javascript code which I assume would be on the same node server. So when the browser would encounter a line like:
<script src="/angularApp.js"></script>
..in your index.html file while parsing it, it would make another request to the node server for http://localhost:<port>/angularApp.js but would get the index.html file as the response as that is what you have set.
Do it like this to serve static files like .html, .css, .js or what have you:
app.use(express.static(path.join(__dirname, '/dist')));

Express 4 route won't hit

const express = require('express');
const path = require('path');
const app = express();
app.listen(9000);
app.get('/test', function(req, res) {
console.log("not being hit");
res.send(200);
});
app.use(express.static(path.join(__dirname, 'build')));
app.get('/*', function (req, res) {
console.log("always hits");
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
This seems like such a simple problem but my brain is starting to turn to mush.
Here's the details:
I have run build on a react app and the index.html file resides in the build folder, and I want this served via express.
I want express to prioritize /test first, and if it's not /test, then I want it to serve the index.html file in the build folder.
If you go to /test, it is skipped and always hits the /* route. If you remove the wild card and use / instead, neither routes will hit if you go to / or /test in the browser. However, index.html is still served and it looks like that's because of the static middleware.
Everything I have read suggests that order in express is important, but I feel like I'm losing my damn mind and I'm starting to slowly descend into madness.
Thanks in advance.

Serving express static files issue

I am having issues w/ serving static files in my current Express app, although I've done a similar setup in a bunch of other apps.. My folder structure is as follows:
/rootfolder/
/app
package.json
/client
/dist
/static
index.html
/server
/src
index.js
Relevant part of my server/src/index.js:
app.use(express.static(path.join(__dirname, "client", "dist")));
Where __dirname = /rootfolder/app/server/src
And when the user hits the / endpoint:
app.get("/", (req, res) => {
res.sendFile(appRoot.path + "/client/dist/index.html");
});
Where appRoot.path = /rootfolder/app
When I hit the / endpoint, I get a status 200 with the following text:
/rootfolder/app/client/dist/index.html
From what I can tell, the files are coded relative to each other correctly.. Does anyone know what I might be doing wrong?
Thanks in advance!
You're using res.send() instead of res.sendFile()
Also I suggest resolving your path via the path module, instead of concatenating a string.
const path = require('path')
app.use(express.static(path.join(__dirname, 'client', 'dist', 'static')))
And for the response of /:
res.sendFile(path.join(__dirname, 'client', 'dist', 'index.html')))
Try
app.use(express.static(path.join(__dirname,'client','dist')));
It basically gets the root directory and combines it with /client+ /dist + /static to give you the full route, without being relative path.
Now Let's call rootdirectory/client/dist X. That is the main directory for static files
If you have other files that are static but not in the same folder, you will have to give relative path from the X directory
Example:
app.get('/',function(req,res){
res.sendFile('/static/data.txt');
}
In the example above you indicate that the static file(data.txt) is located in the X/static directory.
Therefore => rootDirectory/client/dist/static/data.txt
2nd Example:
Let's say you have a folder in dist called images which you want to only store images.
when you are giving routes, you MUST use /images/filename.extention

Heroku ENOENT: no such file or directory due to faulty express.js routing

I seen other people running into the same issue but I am starting to believe that I have a deeper issue with my express file setup since other solutions are not fixing it.
The idea that I had was to have a main page and then add folders for each of my projects. This is my folder structure:
And the code in the index.js is simply this:
const express = require('express');
const app = express();
app.listen(process.env.PORT || 5000, function () {
console.log('Example app listening on port 5000!')
})
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.use(express.static('asteroid'));
app.get('/asteroid', function(req, res){
res.sendFile(__dirname + '/asteroid/');
});
Now like many other question out there, the issue that I see in the log is "Error: ENOENT: no such file or directory, stat '/app/asteroid/index.html'". The app tries to go to /app and it can't find my files on there. I seem to be missing something simple.
Any idea what can be causing this in my case? I tried { root: __dirname }, joining everything with path(), and placed __dirname everywhere with no luck.
Looks like the file listing shows Asteroid uppercase. Maybe try renaming it to be lowercase everywhere?
If you're using express static then you don't need routes for each file. So maybe just use static or just use routes.

Rewrite entire dynamic path with Express 3 and 4

I've got a directory structure like so:
/scripts
server.js
/docs
node_modules
app.css
index.html
Originally I had some static routes to map files in the server.js:
app.get('/', function(request, response){
response.sendFile(path.resolve(basePath, './docs/index.html'));
});
app.get('/app.css', function(request, response){
response.sendFile(path.resolve(basePath, './docs/app.css'));
});
But now I need to globally include any search path under node_modules as well, something like:
app.get('/node_modules*', function(request, response){
response.sendFile(path.resolve(basePath, './docs/( - complete subtree - )'));
});
But that obviously doesn't work. It seems like a URL rewrite is more appropriate here, but I can't figure out how to do that with app.get or with app.configure + some express.static magic.
How can I route an entire directory to a different physical folder, retaining the subpaths?
You can use the express.static to static files.
var path = require('path');
app.use('/node_modules', express.static(path.join(__dirname+'/../docs/node_modules')));
Hope it helps you.

Resources