better handling for async access operations

This commit is contained in:
James Tomasino 2023-07-29 13:04:47 +00:00
parent 97c437cdce
commit a496f77d3a
1 changed files with 16 additions and 12 deletions

View File

@ -132,10 +132,11 @@ app.get('/sitemap.xml', function (_req, res) {
stat('/var/gopher/rss.xml', (err, stats) => {
if (err) {
res(err)
} else {
content = content.replace(/##date##/g, stats.mtime.toISOString())
res.setHeader('content-type', 'text/xml')
res.render('raw', { content: content })
}
content = content.replace(/##date##/g, stats.mtime.toISOString())
res.setHeader('content-type', 'text/xml')
res.render('raw', { content: content })
})
})
@ -157,10 +158,14 @@ app.get('/.well-known/webfinger', function(req, res) {
res.render('raw', { content: content })
}
})
} else {
res.setHeader('content-type', 'application/jrd+json')
res.render('raw', { content: '' })
}
} else {
res.setHeader('content-type', 'application/jrd+json')
res.render('raw', { content: '' })
}
res.setHeader('content-type', 'application/jrd+json')
res.render('raw', { content: '' })
})
// Any link to a direct static resource will show it
@ -193,12 +198,16 @@ app.use(express.static('/var/gopher'))
// Grab anything that's a .txt and wrap it in .html
app.get('*', function(req, res){
let error = false
if (req.path.indexOf('.html') !== -1) {
const file = path.join('/var/gopher/', decodeURIComponent(req.path).replace(/\.html/, '.txt'));
access(file, constants.R_OK, (err) => {
if (err) {
error = true
const back = '<a href="/"><span class="dim">&lt;&lt;</span> BACK TO COSMIC VOYAGE</a>'
const error = 'Message not found. Please try again.'
const content = back + '\n\n' + error
res.status(404)
const fullUrl = 'https://cosmic.voyage' + req.originalUrl
res.render('basic', { content: content, canonical: fullUrl})
} else {
const file = escape(bufferFile('/var/gopher/' + decodeURIComponent(req.path).replace(/\.html/, '.txt')))
const back = '<a href="/log"><span class="dim">&lt;&lt;</span> BACK TO RS001 LOG</a>'
@ -208,11 +217,6 @@ app.get('*', function(req, res){
}
})
} else {
error = true
}
// If resource isn't found, give the 404
if (error) {
const back = '<a href="/"><span class="dim">&lt;&lt;</span> BACK TO COSMIC VOYAGE</a>'
const error = 'Message not found. Please try again.'
const content = back + '\n\n' + error