1
Using Node.JS I want to read a Javascript script in a folder and return it.
It turns out that when I access the file through the browser, the code appears normally.
But the script is not embedded in the HTML page using the tag <script>
I’m using the Express framework. I tried to do this way but it didn’t work.
app.get(/\/.+/, function(req, res){
var file = __dirname + "/www/public" + req.path;
if(fs.existsSync(file)){
if(file.match(/\.js$/)){
res.header("Content-Type", "application/javascript");
}
res.sendFile(file);
res.status(200);
} else {
res.sendFile(__dirname + "/www/404.html");
res.status(404);
}
});
This worked, but Javascript is still not embedded in the HTML page. I open it in the direct link, but on the page is as if the tag
<script>
even existed. No error appears. Detail which CSS file is working correctly, the problem is only with . js– Silva97
ta using ejs or Pug ?
– Eduardo Sampaio
Since I don’t know what this is about, I guess neither of you.
– Silva97
My God, I’m sorry to waste your time... I use Noscript, and I didn’t enable localhost to run code. ' Thank you for showing me this more practical way of giving access to files from a folder.
– Silva97