Posts

Showing posts with the label Express

Accessing EJS Variable In Javascript Logic

Answer : You could directly inject the gameState variable into javascript on the page. <% if (gameState) { %> <h2>I have a game state!</h2> <script> var clientGameState = <%= gameState %> </script> <% } %> Another option might be to make an AJAX call back to the server once the page has already loaded, return the gameState JSON, and set clientGameState to the JSON response. You may also be interested in this: How can I share code between Node.js and the browser? I had the same problem. I needed to use the data not for just rendering the page, but in my js script. Because the page is just string when rendered, you have to turn the data in a string, then parse it again in js. In my case my data was a JSON array, so: <script> var test = '<%- JSON.stringify(sampleJsonData) %>'; // test is now a valid js object </script> Single quotes are there to not be mixed with double-q...

CSS File Blocked: MIME Type Mismatch (X-Content-Type-Options: Nosniff)

Answer : I just ran into the same issue. It appears to be a quirk of Express that can manifest itself for a few different reasons, judging by the number of hits from searching the web for "nodejs express css mime type". Despite the type="text/css" attribute we put in our <link elements, Express is returning the CSS file as Content-Type: "text/html; charset=utf-8" whereas it really should be returning it as Content-Type: "text/css" For me, the quick and dirty workaround was to simply remove the rel= attribute, i.e., change <link rel="stylesheet" type="text/css" href="styles.css"> to <link type="text/css" href="styles.css"> Testing confirmed that the CSS file was downloaded and the styles did actually work, and that was good enough for my purposes. I also removed rel = "stylesheet" , and I no longer get the MIME type error, but the styles are not being loaded Some answ...