Skip to content

Commit fc5bb87

Browse files
committed
Fix: no longer log 404 as error
1 parent bcfae9f commit fc5bb87

1 file changed

Lines changed: 30 additions & 13 deletions

File tree

src/app.js

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,45 @@ const manifestIconTypes = {
265265
'icon-512.png': 'image/png'
266266
};
267267

268+
const appleTouchIconAliases = [
269+
'/apple-touch-icon.png',
270+
'/apple-touch-icon-precomposed.png',
271+
'/apple-touch-icon-120x120.png',
272+
'/apple-touch-icon-120x120-precomposed.png',
273+
];
274+
268275
const sendStaticStorageFile = (res, relativePath, contentType) => {
269276
const filePath = getStaticFilePath(relativePath, { fallbackToPublic: true });
270277
res.header('Content-Type', contentType);
271278
res.header('Cache-Control', 'public, max-age=172800');
272279
res.send(fs.readFileSync(filePath));
273280
};
274281

282+
const sendNotFoundResponse = (req, res) => {
283+
res.status(404);
284+
if (req.accepts('html')) {
285+
ejs.renderFile(path.join(__dirname, '..', 'views', 'error', 'error-xxx.ejs'), { statusCode: 404, message: "Page not found", info: "Request can not be served", reason: "The requested page was not found", domain: process.env.DOMAIN, back_url: process.env.DOMAIN, curentUnixTime: new Date().getTime() }, (err, str) => {
286+
if (err) throw err;
287+
res.header('Content-Type', 'text/html');
288+
res.send(str);
289+
});
290+
} else {
291+
res.header('Content-Type', 'application/json');
292+
res.json({ message: "Page not found", info: "Request can not be served", reason: "The requested page was not found" });
293+
294+
}
295+
};
296+
275297
app.get('/favicon.ico', (req, res) => {
276298
sendStaticStorageFile(res, 'favicon.ico', 'image/x-icon');
277299
});
278300

301+
appleTouchIconAliases.forEach((alias) => {
302+
app.get(alias, (req, res) => {
303+
sendStaticStorageFile(res, path.join('icons', 'icon-192.png'), 'image/png');
304+
});
305+
});
306+
279307
app.get('/icons/:filename', (req, res) => {
280308
const contentType = manifestIconTypes[req.params.filename];
281309
if (!contentType) {
@@ -336,23 +364,12 @@ app.get('/*', (req, res) => {
336364
if (fs.existsSync(resolvedPath)) {
337365
file_to_send = resolvedPath;
338366
} else {
339-
throw new Error(`File not found - ${filePath}`);
367+
return sendNotFoundResponse(req, res);
340368
}
341369
res.send(fs.readFileSync(file_to_send));
342370
} catch (error) {
343371
process.log.error(error)
344-
res.status(404);
345-
if (req.accepts('html')) {
346-
ejs.renderFile(path.join(__dirname, '..', 'views', 'error', 'error-xxx.ejs'), { statusCode: 404, message: "Page not found", info: "Request can not be served", reason: "The requested page was not found", domain: process.env.DOMAIN, back_url: process.env.DOMAIN, curentUnixTime: new Date().getTime() }, (err, str) => {
347-
if (err) throw err;
348-
res.header('Content-Type', 'text/html');
349-
res.send(str);
350-
});
351-
} else {
352-
res.header('Content-Type', 'application/json');
353-
res.json({ message: "Page not found", info: "Request can not be served", reason: "The requested page was not found" });
354-
355-
}
372+
sendNotFoundResponse(req, res);
356373
};
357374
});
358375

0 commit comments

Comments
 (0)