response.end(data)
}
-Server.prototype.die = function(response) {
- let body = '<html><body>Page not found</body></html>'
- this.serve(response, 404, this.mime_type_html, Buffer.from(body))
+Server.prototype.die = function(response, pathname) {
+ this.serve(
+ response,
+ 404,
+ this.mime_type_html,
+ Buffer.from(
+ `<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
+<TITLE>404 Not Found</TITLE></HEAD><BODY>
+<H1>404 Not Found</H1>
+The document ${pathname} was not found.
+</BODY></HTML>
+`
+ )
+ )
}
Server.prototype.redirect = function(response, location) {
response.statusCode = 301
response.setHeader('Location', location)
- response.end('Redirecting to ' + location)
+ this.serve(
+ response,
+ 301,
+ this.mime_type_html,
+ Buffer.from(
+ `<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
+<TITLE>301 Moved</TITLE></HEAD><BODY>
+<H1>301 Moved</H1>
+The document has moved
+<A HREF="${location}">here</A>.
+</BODY></HTML>
+`
+ )
+ )
}
let site_factory_default = async root => new Site(root)
if (!Object.prototype.hasOwnProperty.call(this.sites, parsed_url.hostname)) {
console.log('nonexistent site', parsed_url.hostname)
- this.die(response)
+ this.die(response, parsed_url.pathname)
return
}
let temp = this.sites[parsed_url.hostname]
pathname_pos: 0,
response: response,
request: request,
+ status: 200,
server: this,
site: site.object
}
return this.server.build_cache_email.get(
path,
async result => {
+ let text = await fs_readFile(path, {encoding: 'utf-8'})
console.log('getting', path, 'as email')
- result.value = emailjs.this.server.connect(
- JSON.parse(await fs_readFile(path))
- )
+ result.value = emailjs.this.server.connect(JSON.parse(text))
}
)
}
return this.server.build_cache_json.get(
path,
async result => {
+ let text = await fs_readFile(path, {encoding: 'utf-8'})
console.log('getting', path, 'as json')
- result.value = JSON.parse(await fs_readFile(path))
+ result.value = JSON.parse(text)
}
)
}
return this.server.build_cache_less.get(
path,
async result => {
+ let text = await fs_readFile(path, {encoding: 'utf-8'})
console.log('getting', path, 'as less')
let render = await less.render(
- await fs_readFile(path, {encoding: 'utf-8'}),
+ text,
{
//color: true,
//compress: false,
return this.server.build_cache_text.get(
path,
async result => {
+ let text = await fs_readFile(path, {encoding: 'utf-8'})
console.log('getting', path, 'as text')
- result.value = await fs_readFile(path, {encoding: 'utf-8'})
+ result.value = text
}
)
}
return this.server.build_cache_zip.get(
path,
async result => {
- console.log('getting', path, 'as zip')
- result.value = {}
let zipfile = await yauzl_open(path, {autoClose: false})
+ console.log('getting', path, 'as zip')
+
let entries = []
await new Promise(
(resolve, reject) => {
on('end', () => resolve())
}
)
+
+ result.value = {}
for (let i = 0; i < entries.length; ++i) {
let read_stream = await new Promise(
(resolve, reject) => {
)
}
)
+
let write_stream = new stream_buffers.WritableStreamBuffer()
let data = new Promise(
(resolve, reject) => {
}
)
read_stream.pipe(write_stream)
- let path = '/' + entries[i].fileName
data = await data
+
+ let path = '/' + entries[i].fileName
console.log('entry path', path, 'size', data.length)
result.value[path] = data
}
Site.prototype.ensure_dir = async function(path) {
try {
await fs_mkdir(this.root + path)
+ console.log('create directory', path)
}
catch (err) {
if (err.code !== 'EEXIST') // should check error type
// this is for read/write JSON files
// they will not be reloaded from disk if modified
-Site.prototype.open_json = async function(path, default_value) {
- return /*await*/ this.server.json_cache.open(this.root + path, default_value)
+Site.prototype.read_json = async function(path, default_value) {
+ return /*await*/ this.server.json_cache.read(
+ this.root + path,
+ default_value
+ )
}
-Site.prototype.flush_json = async function(path) {
- return /*await*/ this.server.json_cache.flush(this.root + path)
+Site.prototype.write_json = async function(path, value, timeout) {
+ return /*await*/ this.server.json_cache.write(
+ this.root + path,
+ value,
+ timeout
+ )
}
+Site.prototype.modify_json =
+ async function(path, default_value, modify_func, timeout) {
+ return /*await*/ this.server.json_cache.modify(
+ this.root + path,
+ default_value,
+ modify_func,
+ timeout
+ )
+ }
Site.prototype.serve_jst = async function(env, pathname) {
let jst
console.log(
`${env.parsed_url.host} serving ${env.pathname} length ${data.length} from jst`
)
- this.server.serve(env.response, 200, env.mime_type, data)
+ this.server.serve(env.response, env.status, env.mime_type, data)
return true
}
console.log(
`${env.parsed_url.host} serving ${env.pathname} length ${data.length} from less`
)
- this.server.serve(env.response, 200, env.mime_type, data)
+ this.server.serve(env.response, env.status, env.mime_type, data)
return true
}
console.log(
`${env.parsed_url.host} serving ${env.pathname} length ${data.length} from fs`
)
- this.server.serve(env.response, 200, env.mime_type, data)
+ this.server.serve(env.response, env.status, env.mime_type, data)
return true
}
console.log(
`${env.parsed_url.host} serving ${env.pathname} length ${data.length} from zip`
)
- this.server.serve(env.response, 200, env.mime_type, data)
+ this.server.serve(env.response, env.status, env.mime_type, data)
return true
}
console.log(
`${env.parsed_url.host} empty directory name in ${env.pathname}`
)
- this.server.die(env.response)
+ this.server.die(env.response, env.pathname)
}
return
}
console.log(
`${env.parsed_url.host} bad component "${filename}" in ${env.pathname}`
)
- this.server.die(env.response)
+ this.server.die(env.response, env.pathname)
return
}
console.log(
`${env.parsed_url.host} non-directory filetype "${filetype}" in ${env.pathname}`
)
- this.server.die(env.response)
+ this.server.die(env.response, env.pathname)
return
}
env.mime_type = this.server.mime_types[filetype]
console.log(
`${env.parsed_url.host} directory not found ${pathname}`
)
- this.server.die(env.response)
+ this.server.die(env.response, env.pathname)
return
}
if (!stats.isDirectory()) {
`${env.parsed_url.host} not directory ${pathname}` :
`${env.parsed_url.host} unknown filetype "${filetype}" in ${pathname}`
)
- this.server.die(env.response)
+ this.server.die(env.response, env.pathname)
return
}
}
console.log(
`${env.parsed_url.host} file not found ${env.pathname}`
)
- this.server.die(env.response)
+ this.server.die(env.response, env.pathname)
}
}