I was doing tests, and, in one of them, I have checked the box disable cache
in the network tools tab of the browser. Since then, express.static
it has stopped serving me 1 . Always the same.
Of course, the file is still in the same place:
static/
webix.js
index.html
navigo.min.js
drivers/
webixui.js
misc.js
navigorouter.js
My server code is:
// Las rutas que uso:
app.post( '/config.json', function( req, res ) {
res.json( require( './app/config/main.js' ) );
res.end( );
} );
app.use( function( req, res, next ) {
console.log( req.url );
next( );
}, express.static( path.join( __dirname, 'static' ) ) );
Simple as the mechanism of a pacifier. The part of console.log( )
the I have put to check the requests that I am receiving:
/webix.js
/navigo.min.js
/drivers/webixui.js
/drivers/misc.js
/drivers/navigorouter.js
/skins/mini.min.css
It is noted that if I receive the request. However, the browser does not get a response to it.
The upload mechanism is custom : I get through POST
a JSON
, and one of its fields contains the files .js
to upload. The code for this is:
if( 'js' in config ){
config.js.forEach( function( item ) {
++loadsCount;
const ref = document.createElement( 'script' );
ref.onload = resourceLoaded;
document.head.appendChild( ref );
ref.src = item;
console.log( `Begin src ${item}` );
} );
}
Through the console.log( )
I see that indeed the request is being made:
Begin src webix.js (index):94
Begin src navigo.min.js (index):94
Begin src drivers/webixui.js (index):94
Begin src drivers/misc.js (index):94
Begin src drivers/ navigorouter.js (index):94
Begin href skins/mini.min.css (index):112
And in the tab Herramientas de Red
I can confirm it: the browser requests everything it has to request:
Examining the headers sent by the browser, I don't see anything unusual :
With all that, in principle I rule out any problem in the browser part. I have verified (at the beginning of the question) that the node.js
is correctly receiving the requests. And my server does not respond with a 404
or any type of error; it just doesn't send a response for that particular file.
I have cleared the browser cache, but there is no way. That file has not been served again. And when using another browser, something similar happens: the file has been served only 1 time , the first time. And from there, the observed behavior is repeated: the file is not served.
How can I fix this ghost file ?
ISSUE
You want to dynamically load a series of static files through requests to an Express / NodeJS server , however there are problems loading the static files, particularly loading one of them.
When analyzing the code that handles the sending of the dynamic configuration (file
.json
), the following is observed:As you can see you have 2 calls to different methods:
res.json()
andres.end()
in the same requestpost
. This causes something called a race condition , since the methodres.json()
must convert the object received as an argument to JSON format, and at the same time, the request is finished withres.end()
, terminating the request/response process, leaving the process of theres.json()
unfinished method , therefore the file would not be sent correctlyjson
to the client.SOLUTION
The solution is simple, remove the call to
res.end()
from your middleware, leaving the code with just:In this way, the request/response process will end correctly, the file being sent
json
to the client so that it can dynamically load the resources of thestatic
Express folder.According to the documentation: (own translation)
That said, there's no need to explicitly call the method
res.end()
when it's usedres.json()
since the latter ends the response process internally.