Download a file from NodeJS Server using Express

后端 未结 7 1694
醉梦人生
醉梦人生 2020-11-22 03:10

How can I download a file that is in my server to my machine accessing a page in a nodeJS server?

I\'m using the ExpressJS and I\'ve been trying this:



        
7条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 03:21

    For static files like pdfs, Word docs, etc. just use Express's static function in your config:

    // Express config
    var app = express().configure(function () {
        this.use('/public', express.static('public')); // <-- This right here
    });
    

    And then just put all your files inside that 'public' folder, for example:

    /public/docs/my_word_doc.docx
    

    And then a regular old link will allow the user to download it:

    My Word Doc
    

提交回复
热议问题