this.enable_caching = false
this.listen = []
- this.mime_types = {}
this.hosts = {}
this.roots = {}
- this.mime_type_html = 'text/html; charset=utf-8'
- this.mime_type_default = 'application/octet-stream'
-
this.resources = new Resources()
this.jst_cache = this.resources.ref(
'jst_cache:.',
this.listen = listen
- this.mime_types = config.mime_types
- this.mime_type_html =
- Object.prototype.hasOwnProperty.call(this.mime_types, '.html') ?
- this.mime_types['.html'] :
- this.mime_type_default
-
this.hosts = config.hosts
}
}
-Server.prototype.serve = function(response, status, mime_type, data, message) {
- console.log(message)
+Server.prototype.serve_internal = function(response, status, mime_type, data) {
response.statusCode = status
- // html files will be direct recipient of links/bookmarks so can't have
- // a long lifetime, other files like css or images are often large files
- // and won't change frequently (but we'll need cache busting eventually)
- if (this.enable_caching && mime_type !== this.mime_type_html)
- response.setHeader('Cache-Control', 'max-age=3600')
+ // no real need to cache errors and hostname redirects
+ // (pathname redirects on the other hand will be cached, see Site.js)
response.setHeader('Content-Type', mime_type)
response.setHeader('Content-Length', data.length)
response.end(data)
}
Server.prototype.die = function(response, pathname, message) {
- this.serve(
+ console.log(message)
+ this.serve_internal(
response,
404,
- this.mime_type_html,
+ 'text/html; charset=utf-8',
Buffer.from(
`<html>
<head>
</body>
</html>
`
- ),
- message
+ )
)
}
Server.prototype.redirect = function(response, location, message) {
+ console.log(message)
response.statusCode = 301
response.setHeader('Location', location)
- this.serve(
+ this.serve_internal(
response,
301,
- this.mime_type_html,
+ 'text/html; charset=utf-8',
Buffer.from(
`<html>
<head>
</body>
</html>
`
- ),
- message
+ )
)
}
new_host += ':' + parsed_url.port
this.redirect(
response,
- `${parsed_url.protocol}//${redirect}${request.url}`,
+ `${parsed_url.protocol}//${new_host}${request.url}`,
`redirecting ${parsed_url.host} to ${new_host}`
)
return
}
catch (err) {
let message = (err.stack || err.message).toString()
- this.serve(
+ console.log(message)
+ this.serve_internal(
response,
500,
- this.mime_type_html,
- Buffer.from(`<html><body><pre>${message}</pre></body></html>`),
- message
+ 'text/html; charset=utf-8',
+ Buffer.from(`<html><body><pre>${message}</pre></body></html>`)
)
}
}