Answer the question
In order to leave comments, you need to log in
How to transfer static resources to the client?
There is this code
<link href="css/custom-theme/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>
<script src="js/window.js"></script>
http.createServer(function (request, response) {
var path = url.parse(request.url).pathname;
switch (path) {
case '/':
sendPage(response);
break;
default:
var fileOpt = "utf8";
if (path.substring(path.length - 3) == "jpg")
fileOpt = "binary";
fs.readFile("."+path, fileOpt, function(err, data) {
if (err) {
console.log(err);
response.writeHead("404", {"Content-Type": "text/plain"})
response.write('File not found!');
} else {
console.log(path);
console.log(fileOpt);
console.log(getMIME(path));
response.writeHead("200", getMIME(path));
response.write(data);
response.end;
}
});
break;
}
}).listen(8888);
/css/custom-theme/jquery-ui-1.9.2.custom.css
utf8
{ 'Content-Type': 'text/css' }
/js/jquery-1.8.3.js
utf8
{ 'Content-Type': 'application/x-javascript' }
/js/jquery-ui-1.9.2.custom.js
utf8
{ 'Content-Type': 'application/x-javascript' }
/js/window.js
utf8
{ 'Content-Type': 'application/x-javascript' }
/img/2.jpg
binary
{ 'Content-Type': 'image/jpeg' }
/img/3.jpg
binary
{ 'Content-Type': 'image/jpeg' }
/img/4.jpg
binary
{ 'Content-Type': 'image/jpeg' }
/img/5.jpg
binary
{ 'Content-Type': 'image/jpeg' }
Answer the question
In order to leave comments, you need to log in
You don't say that the answer is complete. You need to call response.end(); at the end, and you have response.end;
1. Your decision to serve static content is a potential security hole. This approach allows you to read files outside the document root if you specify a path with a jump to the parent directory ('../').
2. Use serve-static ( https://github.com/expressjs/serve-static) as an option or Express 4.
3. In general, static should be given to those that do it best. Use Nginx for this.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question