examples/downloads/index.js JAVASCRIPT 41 lines View on github.com → Search inside
1'use strict'23/**4 * Module dependencies.5 */67var express = require('../../');8var path = require('node:path');910var app = module.exports = express();1112// path to where the files are stored on disk13var FILES_DIR = path.join(__dirname, 'files')1415app.get('/', function(req, res){16  res.send('<ul>' +17    '<li>Download <a href="/files/notes/groceries.txt">notes/groceries.txt</a>.</li>' +18    '<li>Download <a href="/files/amazing.txt">amazing.txt</a>.</li>' +19    '<li>Download <a href="/files/missing.txt">missing.txt</a>.</li>' +20    '<li>Download <a href="/files/CCTV大赛上海分赛区.txt">CCTV大赛上海分赛区.txt</a>.</li>' +21    '</ul>')22});2324// /files/* is accessed via req.params[0]25// but here we name it :file26app.get('/files/*file', function (req, res, next) {27  res.download(req.params.file.join('/'), { root: FILES_DIR }, function (err) {28    if (!err) return; // file sent29    if (err.status !== 404) return next(err); // non-404 error30    // file for download not found31    res.statusCode = 404;32    res.send('Cant find that file, sorry!');33  });34});3536/* istanbul ignore next */37if (!module.parent) {38  app.listen(3000);39  console.log('Express started on port 3000');40}

Code quality findings 6

Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var express = require('../../');
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var path = require('node:path');
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var app = module.exports = express();
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var FILES_DIR = path.join(__dirname, 'files')
Use strict equality (===) to prevent type coercion bugs
info correctness loose-equality
if (err.status !== 404) return next(err); // non-404 error
Remove debugging statements or use a logging library
info correctness console-log
console.log('Express started on port 3000');

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.