Reputation: 2003
I'm running a webserver that renders jade files. In the jade files I have some javascript files. The page renders fine but the javascript files are not being found.
Node server
var http = require('http')
, url = require('url')
, fs = require('fs')
, jade = require('jade')
, server;
server = http.createServer(function(req, res) {
var path = url.parse(req.url).pathname;
switch (path) {
case '/audio':
var options = {pretty: true};
jade.renderFile('myJadeFile.jade', options, function(err, html) {
if (err) return send404(res);
res.writeHead(200, {"Content-Type": "text/html"});
res.write(html, 'utf8');
res.end();
});
break;
default: send404(res);
}
});
function send404(res) {
res.writeHead(404);
res.write('404');
res.end();
}
server.listen(3000, function() {
console.log("listening on port:3000");
});
Jade File
!!! 5
html(lang="en")
head
meta(charset="UTF-8")
title Please Work
script(src="myJSFile.js")
body
#container
h1 It worked!
I get this error:
GET http://localhost:3000/myJSFile.js 404 (Not Found)
It's not finding the file and I'm not sure what I have to put on the serverside jade options to find the javascript file. All files are in the same folder.
Any suggestions?
Upvotes: 0
Views: 810
Reputation: 161637
Jade is an HTML rendering tool, so it is rendering a standard <script>
tag. It does not compile the JS into the HTML file itself. You will need to provide handlers in your case
to handle any dependent files so that the browser can fetch them.
Here's an example for this specific case, assuming that you have myJSFile.js in the same directory as your server file.
case '/myJSFile.js':
fs.createReadStream(__dirname + path).pipe(res);
break;
Upvotes: 1