Web framework.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

static.js 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var fs = require("fs");
  2. var pathlib = require("path");
  3. function mimetype(path) {
  4. }
  5. module.exports = function(root, before) {
  6. return function(req, res, app) {
  7. var pn = req.urlobj.pathname;
  8. // Send a file
  9. function send(path) {
  10. var rs = fs.createReadStream(path);
  11. rs.on("error", err => {
  12. app.notice(err);
  13. res.end(template(app.res404, { pathname: pathname }));
  14. });
  15. rs.on("data", d => res.write(d));
  16. rs.on("end", () => res.end());
  17. }
  18. res.responded = true;
  19. // Prevent leaking information
  20. if (pn.indexOf("../") !== -1 || pn.indexOf("/..") !== -1 || pn === "..") {
  21. res.writeHead(403);
  22. res.end(template(app.res403, { pathname: pn }));
  23. return;
  24. }
  25. // Join the web root with the request's path name
  26. var path = pathlib.join(root, pn.replace(before, ""));
  27. fs.stat(path, (err, stat) => {
  28. // If there's an error stat'ing, just error
  29. if (err) {
  30. app.notice(err);
  31. res.writeHead(404);
  32. res.end(template(app.res404, { pathname: pn }));
  33. return;
  34. }
  35. // If it's a directory, we want the index.html file
  36. if (stat.isDirectory())
  37. path = pathlib.join(path, "index.html");
  38. // Send the file
  39. send(path, pn, res, app);
  40. });
  41. }
  42. }