Skip to content
Myr

index.js

This file

Path

examples/downloads/index.js

40 lines · 1.2 KiB · JavaScript

Last changed by

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

History of this file

Take it

The walk stopped after 200 commits: lines older than 2027b87 point at it.

fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87

lines 1–7why, one step back
1'use strict'
23/**4 * Module dependencies.5 */67var express = require('../../');

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

line 8why, one step back
8var path = require('node:path');

fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87

lines 9–40why, one step back
9
10var 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}