server: define sendFile as server method once and for all
This commit is contained in:
parent
a425d8156c
commit
08d4139844
1 changed files with 22 additions and 21 deletions
|
|
@ -129,27 +129,28 @@ class WebServer extends EventEmitter {
|
|||
|
||||
}
|
||||
|
||||
httpRoute(req, res) {
|
||||
sendFile(req, res, path) {
|
||||
|
||||
res.sendFile = (path)=>{
|
||||
var fpath = path.split('?')[0]
|
||||
var stats = fs.statSync(fpath)
|
||||
if (!stats || !stats.isFile()) {
|
||||
throw `File "${fpath}" not found.`
|
||||
}
|
||||
var lastModified = new Date(stats.mtime)
|
||||
lastModified.setMilliseconds(0)
|
||||
res.setHeader('Cache-Control', 'no-cache')
|
||||
res.setHeader('Last-Modified', lastModified.toUTCString())
|
||||
if (
|
||||
req.headers['if-modified-since'] && new Date(req.headers['if-modified-since']).getTime() >= lastModified.getTime()
|
||||
) {
|
||||
res.statusCode = 304
|
||||
res.end()
|
||||
return
|
||||
}
|
||||
return send(req, fpath).pipe(res)
|
||||
var fpath = path.split('?')[0]
|
||||
var stats = fs.statSync(fpath)
|
||||
if (!stats || !stats.isFile()) {
|
||||
throw `File "${fpath}" not found.`
|
||||
}
|
||||
var lastModified = new Date(stats.mtime)
|
||||
lastModified.setMilliseconds(0)
|
||||
res.setHeader('Cache-Control', 'no-cache')
|
||||
res.setHeader('Last-Modified', lastModified.toUTCString())
|
||||
if (
|
||||
req.headers['if-modified-since'] && new Date(req.headers['if-modified-since']).getTime() >= lastModified.getTime()
|
||||
) {
|
||||
res.statusCode = 304
|
||||
res.end()
|
||||
return
|
||||
}
|
||||
return send(req, fpath).pipe(res)
|
||||
}
|
||||
|
||||
httpRoute(req, res) {
|
||||
|
||||
var url = req.url
|
||||
|
||||
|
|
@ -218,7 +219,7 @@ class WebServer extends EventEmitter {
|
|||
// osc asset files
|
||||
|
||||
try {
|
||||
res.sendFile(path.resolve(__dirname + '/../../' + url.replace('__APP_DIR__/', '')))
|
||||
this.sendFile(req, res, path.resolve(__dirname + '/../../' + url.replace('__APP_DIR__/', '')))
|
||||
return true
|
||||
} catch(e) {}
|
||||
|
||||
|
|
@ -255,7 +256,7 @@ class WebServer extends EventEmitter {
|
|||
|
||||
if (resolvedPath) {
|
||||
try {
|
||||
res.sendFile(resolvedPath)
|
||||
this.sendFile(req, res, resolvedPath)
|
||||
return true
|
||||
} catch(e) {}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue