mirror of
https://github.com/azures04/crafatar.git
synced 2026-03-21 23:41:18 +01:00
use new response module for assets
unfortunately we can't use stream pipes because we need to generate a hash of the content for the Etag. I think proper caching (i.e. Etag) is very important
This commit is contained in:
parent
2e66e5c794
commit
69f0ee23be
@ -18,20 +18,20 @@ var routes = {
|
|||||||
capes: require("./routes/capes")
|
capes: require("./routes/capes")
|
||||||
};
|
};
|
||||||
|
|
||||||
function asset_request(req, res) {
|
// serves assets from lib/public
|
||||||
|
function asset_request(req, callback) {
|
||||||
var filename = path.join(__dirname, "public", req.url.path_list.join("/"));
|
var filename = path.join(__dirname, "public", req.url.path_list.join("/"));
|
||||||
fs.exists(filename, function(exists) {
|
fs.exists(filename, function(exists) {
|
||||||
if (exists) {
|
if (exists) {
|
||||||
res.writeHead(200, {
|
fs.readFile(filename, function(err, data) {
|
||||||
"Content-type": mime.lookup(filename),
|
callback({
|
||||||
"Cache-Control": "max-age=7200, public", // cache for 2 hours
|
body: data,
|
||||||
|
type: mime.lookup(filename),
|
||||||
|
err: err
|
||||||
|
});
|
||||||
});
|
});
|
||||||
fs.createReadStream(filename).pipe(res);
|
|
||||||
} else {
|
} else {
|
||||||
res.writeHead(404, {
|
callback({});
|
||||||
"Content-type": "text/plain"
|
|
||||||
});
|
|
||||||
res.end("Not Found");
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -95,7 +95,9 @@ function requestHandler(req, res) {
|
|||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
asset_request(req, res);
|
asset_request(req, function(result) {
|
||||||
|
response(req, res, result);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
var error = JSON.stringify(req.headers) + "\n" + e.stack;
|
var error = JSON.stringify(req.headers) + "\n" + e.stack;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user