UNPKG

4.02 kBJavaScriptView Raw
1
2
3var path = require("path")
4
5
6module.exports = function createStatic(_root, _opts) {
7 var root = path.resolve(_root)
8 , opts = Object.assign({
9 index: "index.html",
10 maxAge: 31536000, // One year
11 rangeSize: 500 * 1024,
12 cacheControl: {
13 "cache.manifest": 0,
14 "worker.js": 0
15 }
16 }, _opts)
17
18 if (opts.cacheControl) {
19 Object.each(opts.cacheControl, resolveFile, opts.cacheControl = {})
20 }
21
22 if (opts.headers) {
23 Object.each(opts.headers, resolveFile, opts.headers = {})
24 }
25
26 return function(req, res, next) {
27 var file
28 , method = req.method
29
30 if (method !== "GET" && method !== "HEAD") {
31 res.setHeader("Allow", "GET, HEAD")
32 return res.sendStatus(405) // Method not allowed
33 }
34
35 if (req.url === "/" && !opts.index) {
36 return res.sendStatus(404)
37 }
38
39 try {
40 file = path.resolve(root, (
41 req.url === "/" ?
42 opts.index :
43 "." + decodeURIComponent(req.url.split("?")[0].replace(/\+/g, " "))
44 ))
45 } catch (e) {
46 return res.sendStatus(400)
47 }
48
49 if (file.slice(0, root.length) !== root) {
50 return res.sendStatus(404)
51 }
52 res.sendFile(file, opts, function(err) {
53 if (err && (err.name === "EISDIR" || err.name === "ERANGE" )) {
54 res.sendStatus(err.code)
55 } else {
56 next()
57 }
58 })
59 }
60
61 function resolveFile(val, file) {
62 this[file === "*" ? file : path.resolve(root, file)] = val
63 }
64}
65
66
67
68
69/*
70
71300 Multiple Choices
72--------------------
73// https://www.w3.org/Style/Examples/007/figures.h
74
75The document name you requested (/Style/Examples/007/figures.) could not be found on this server. However, we found documents with names similar to the one you requested.
76Available documents:
77 - /Style/Examples/007/figures.it.html.redirect (common basename)
78
79Please consider informing the owner of the __referring page__ about the broken link.
80
81 - It is possible to communicate the list using a set of Link header fields [RFC5988],
82 each with a relationship of "alternate".
83 - https://tools.ietf.org/html/rfc2295 Transparent Content Negotiation in HTTP
84 https://tools.ietf.org/html/draft-ietf-http-alternates-01
85 Alternates: {"http://x.org/paper.1" 0.9 {type text/html} {language en}}, {"http://x.org/paper.2" 0.7 {type text/html} {language fr}}
86
87
88406 Not Acceptable
89------------------
90
91It is not what anyone would expect, but if you're sure:
92you can for example use a custom header like Accept-confirm: confirm-delete, and return 406 Not Acceptable if the Accept-confirm header is not what you expect.
93
94
95This is done through the following code:
96
97<a href="javascript:void(0);"
98 onclick="document.execCommand('SaveAs',true,'file.html');"
99 >Save this page</a>
100
101 However, usually you want to save another file, the file a hyperlink leads to. To do that javascript is not enough (at least there is no such standard way) and something must be done on the server.
102 Forcing SaveAs using the HTTP header
103
104 In order to force the browser to show SaveAs dialog when clicking a hyperlink you have to include the following header in HTTP response of the file to be downloaded:
105
106 Content-Disposition: attachment; filename="<file name.ext>"
107
108 Where <file name.ext> is the filename you want to appear in SaveAs dialog (like finances.xls or mortgage.pdf) - without < and > symbols.
109
110 You have to keep the following in mind:
111
112 The filename should be in US-ASCII charset and shouldn't contain special characters: < > \ " / : | ? * space.
113 The filename should not have any directory path information specified.
114 The filename should be enclosed in double quotes but most browsers will support file names without double quotes.
115 Ancient browsers also required the following (not needed nowadays, but for a fool proof solution might be worth doing):
116
117 Content-Type header should be before Content-Disposition.
118 Content-Type header should refer to an unknown MIME type (at least until the older browsers go away).
119
120
121
122Try downloading files with Content-disposition
123
124Try the header support in your browser, click here:
125
126Text file with Content-Type of application/x-unknown.
127
128*/
129
130
131
132