xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

express.js res.download not working All In One

express.js res.download not working All In One

express.js 4.x

https://expressjs.com/en/api.html

5.x

https://expressjs.com/en/5x/api.html

https://github.com/expressjs/express/blob/5.0/History.md

res.download

var fs = require('fs');
var path = require("path");

var express = require("express");
var app = express();

const log = console.log;

app.get("/download/:filename", (req, res) => {
  const filename = req.params.filename;
  const filePath = path.resolve(__dirname, `./imgs/${filename}`);
  console.log('filePath =', filePath);
  res.download(filePath,`${filename}`, (err) => {
    if (err) {
      res.send({
          error : err,
          msg: "download file error"
      });
    }
    // ❌ {"error":{"errno":-2,"code":"ENOENT","syscall":"stat","path":"/Users/xgqfrms-mbp/Documents/GitHub/node.js/download-preview/src/imgs/logo.png","expose":false,"statusCode":404,"status":404},"msg":"download file error"}
  });
  // res.status(200);
});


const port = 3000;
app.listen(port, function(){
  console.log(`server is running: http://localhost:${port}`);
});

// http://localhost:3000/download/logo.png

app.get("/download/:filename", (req, res) => {
  const filename = req.params.filename;
  // const filePath = __dirname + `/imgs/${filename}`;
  // const filePath = path.resolve(__dirname, `./imgs/${filename}`);
  // const filePath = path.join(__dirname, `./imgs/${filename}`);
  const filePath = `./imgs/${filename}`;
  console.log('__dirname =', __dirname);
  console.log('filePath =', filePath);
  // const url = `https://cdn.xgqfrms.xyz/logo/icon.png`;
  // ❌
  // res.download(url);
  // Error: ENOENT: no such file or directory, stat '/Users/xgqfrms/node.js/download-preview/https:/cdn.xgqfrms.xyz/logo/icon.png'
  // ✅
  // res.download(filePath);
  // ✅
  res.download(filePath, `file-without-extension`);
  // ✅
  // res.download(filePath,`${filename}`, (err) => {
  //   if (err) {
  //     res.send({
  //         error : err,
  //         msg: "download file error"
  //     });
  //   }
  // });
  // res.status(200);
});

https://github.com/expressjs/express/issues/3204#issuecomment-1100824787

https://expressjs.com/en/api.html#res.download

Node.js path


// commonjs
const path = require('path')

// esm
import path form 'path';


CJS

var fs = require("fs");
var path = require("path");

const log = console.log;
console.log('commonjs __dirname =', __dirname);
// commonjs __dirname = /Users/xgqfrms-mbp/Documents/GitHub/app/src

// / 当前文件路径下创建文件夹 ✅  (./src => ./src/upload)
// const dir = __dirname + '/upload';
// console.log('commonjs dir =', dir);
// commonjs dir = /Users/xgqfrms-mbp/Documents/GitHub/app/src/upload

// ../ 当前文件上一层路径下创建文件夹 ❌ (./src => ./upload)
// const dir = __dirname + '../upload';
// console.log('commonjs dir =', dir);
// Error: ENOENT: no such file or directory, mkdir '/Users/xgqfrms-mbp/Documents/GitHub/app/src../upload'

// ../ 当前文件上一层路径下创建文件夹 ✅  (./src => ./upload)
// const dir = path.resolve('../upload');
const dir = path.resolve(__dirname, '../upload');
console.log('commonjs dir =', dir);
// commonjs dir = /Users/xgqfrms-mbp/Documents/GitHub/app/upload


if (!fs.existsSync(dir)) {
  fs.mkdirSync(dir);
}

// if (!fs.existsSync(dir)) {
//   fs.mkdirSync(dir, {
//     mode: 0o744,
//   });
//   // mode's default value is 0o744
// }


// if (!fs.existsSync(dir)) {
//   fs.mkdirSync(dir, {
//     recursive: true,
//     mode: 0o744,
//   });
//   // mode's default value is 0o744
// }


/*

// mjs
node --experimental-modules ./src/mkdir.mjs

# "type": "module",
node ./src/mkdir.js

node ./src/mkdir.cjs

*/

ESM

import fs from "fs";
import path from "path";


const log = console.log;

// 指定当前 root 路径 ✅
const __dirname = path.resolve('./src');

console.log('esm __dirname =', __dirname);
// esm __dirname = /Users/xgqfrms-mbp/Documents/GitHub/app

// ../ 当前文件上一层路径下创建文件夹 ✅  (./src => ./upload)
const dir = path.resolve(__dirname, '../upload');
log('esm dir =', dir);
// esm dir = /Users/xgqfrms-mbp/Documents/GitHub/app/upload

// ../ 当前文件上一层路径下创建文件夹 ❌  (./src => ./upload)
// const dir = __dirname + '../upload';
// log('esm dir =', dir);
// Error: ENOENT: no such file or directory, mkdir '/Users/xgqfrms-mbp/Documents/GitHub/app/src../upload'

if (!fs.existsSync(dir)) {
  fs.mkdirSync(dir);
}

// if (!fs.existsSync(dir)) {
//   fs.mkdirSync(dir, {
//     mode: 0o744,
//   });
//   // mode's default value is 0o744
// }

// if (!fs.existsSync(dir)) {
//   fs.mkdirSync(dir, {
//     recursive: true,
//     mode: 0o744,
//   });
//   // mode's default value is 0o744
// }


/*

node --experimental-modules ./src/mkdir-src.mjs

node ./src/mkdir-src.js

*/

https://nodejs.dev/learn/nodejs-file-paths

https://nodejs.dev/learn/the-nodejs-path-module

https://nodejs.org/api/path.html

refs

https://www.cnblogs.com/xgqfrms/p/16155641.html



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-04-17 15:52  xgqfrms  阅读(164)  评论(5编辑  收藏  举报