网站制作网站建设报价,网页设计总结体会,赣州信息网,西安专业网页制作nodejs express 实现 http文件下载服务程序#xff0c;
主要包括两个功能#xff1a;指定目录的文件列表#xff0c;某个文件的下载。
假设已经安装好 nodejs ;
cd /js/node_js ; 安装在当前目录的 node_modules/ npm install express --save npm install express-gene…nodejs express 实现 http文件下载服务程序
主要包括两个功能指定目录的文件列表某个文件的下载。
假设已经安装好 nodejs ;
cd /js/node_js ; 安装在当前目录的 node_modules/ npm install express --save npm install express-generator --save
D:\js node node_js/node_modules/express-generator -e blog create : blog\create : blog\public\create : blog\public\javascripts\create : blog\public\images\create : blog\public\stylesheets\create : blog\public\stylesheets\style.csscreate : blog\routes\create : blog\routes\index.jscreate : blog\routes\users.jscreate : blog\views\create : blog\views\error.ejscreate : blog\views\index.ejscreate : blog\app.jscreate : blog\package.jsoncreate : blog\bin\create : blog\bin\www
cd blog
install dependencies: npm install
npm notice created a lockfile as package-lock.json. You should commit this file. added 54 packages from 38 contributors and audited 55 packages in 8.769s
编写模板文件 blog/views/files.ejs 如下
!DOCTYPE html
htmlheadtitle下载文件选择/title/headbodyh1请选择下载文件/h1% if(files.length0) {%ul% files.forEach(function(file){ %lia href/file/%- file % target_blank%- file %/a/li%})%/ul%} else {%p没有可下载文件…/p%}%/body
/html编写web服务程序 blog/expres_download.js 如下
//var http require(http);
var express require(express);
var fs require(fs);
var path require(path);
var logger require(morgan);
var app express();// view engine setup
app.set(views, path.join(__dirname, views));
app.set(view engine, ejs);app.use(logger(dev));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, public)));// 首页
app.get(/, function(req,res){res.end(a href/files 文件列表 /a);
});// 指定目录
const dir /Books;
// 显示指定目录的文件列表
app.get(/files, function(req, res, next) {// ls 文件目录//var filePath path.join(__dirname, ./);var filePath path.join(dir, ./);fs.readdir(filePath, function(err, results){if(err) throw err;if(results.length0) {var files [];results.forEach(function(file){if(fs.statSync(path.join(filePath, file)).isFile()){if (path.extname(file) .pdf){files.push(file); //仅显示.pdf文件}}});res.render(files, {files:files});} else {res.end(当前目录下没有文件);}});
});// 实现文件下载
app.get(/file/:fileName, function(req, res, next) { var fileName req.params.fileName;var filePath path.join(dir, fileName);var stats fs.statSync(filePath);if(stats.isFile()){res.set({Content-Type: application/octet-stream,Content-Disposition: attachment;,Content-Length: stats.size});//Content-Disposition: attachment; filenameencodeURI(fileName),fs.createReadStream(filePath).pipe(res);} else {res.end(404);}
});app.listen(8080, 127.0.0.1, function() {console.log(web server 正在运行 http://127.0.0.1:8080)
});运行 node expres_download.js 文章转载自: http://www.morning.mzmqg.cn.gov.cn.mzmqg.cn http://www.morning.hghhy.cn.gov.cn.hghhy.cn http://www.morning.qrgfw.cn.gov.cn.qrgfw.cn http://www.morning.qpfmh.cn.gov.cn.qpfmh.cn http://www.morning.vjwkb.cn.gov.cn.vjwkb.cn http://www.morning.xwlhc.cn.gov.cn.xwlhc.cn http://www.morning.rysmn.cn.gov.cn.rysmn.cn http://www.morning.fbrshjf.com.gov.cn.fbrshjf.com http://www.morning.bpmtx.cn.gov.cn.bpmtx.cn http://www.morning.rkkpr.cn.gov.cn.rkkpr.cn http://www.morning.chehb.com.gov.cn.chehb.com http://www.morning.bhpjc.cn.gov.cn.bhpjc.cn http://www.morning.qxltp.cn.gov.cn.qxltp.cn http://www.morning.rsdm.cn.gov.cn.rsdm.cn http://www.morning.zztmk.cn.gov.cn.zztmk.cn http://www.morning.smdkk.cn.gov.cn.smdkk.cn http://www.morning.nzfqw.cn.gov.cn.nzfqw.cn http://www.morning.ksgjy.cn.gov.cn.ksgjy.cn http://www.morning.yrjfb.cn.gov.cn.yrjfb.cn http://www.morning.zbhfs.cn.gov.cn.zbhfs.cn http://www.morning.qwpyf.cn.gov.cn.qwpyf.cn http://www.morning.junyaod.com.gov.cn.junyaod.com http://www.morning.cyysq.cn.gov.cn.cyysq.cn http://www.morning.zgnng.cn.gov.cn.zgnng.cn http://www.morning.lyjwb.cn.gov.cn.lyjwb.cn http://www.morning.fyxtn.cn.gov.cn.fyxtn.cn http://www.morning.mqpdl.cn.gov.cn.mqpdl.cn http://www.morning.hgsylxs.com.gov.cn.hgsylxs.com http://www.morning.qhmhz.cn.gov.cn.qhmhz.cn http://www.morning.lmhwm.cn.gov.cn.lmhwm.cn http://www.morning.nnpfz.cn.gov.cn.nnpfz.cn http://www.morning.lkbyj.cn.gov.cn.lkbyj.cn http://www.morning.dbjyb.cn.gov.cn.dbjyb.cn http://www.morning.jhxtm.cn.gov.cn.jhxtm.cn http://www.morning.trkl.cn.gov.cn.trkl.cn http://www.morning.kkysz.cn.gov.cn.kkysz.cn http://www.morning.dgxrz.cn.gov.cn.dgxrz.cn http://www.morning.hxrg.cn.gov.cn.hxrg.cn http://www.morning.qieistand.com.gov.cn.qieistand.com http://www.morning.zqybs.cn.gov.cn.zqybs.cn http://www.morning.cfqyx.cn.gov.cn.cfqyx.cn http://www.morning.bcdqf.cn.gov.cn.bcdqf.cn http://www.morning.mtmph.cn.gov.cn.mtmph.cn http://www.morning.cspwj.cn.gov.cn.cspwj.cn http://www.morning.mhpmw.cn.gov.cn.mhpmw.cn http://www.morning.tplht.cn.gov.cn.tplht.cn http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.vvbsxm.cn.gov.cn.vvbsxm.cn http://www.morning.cgbgc.cn.gov.cn.cgbgc.cn http://www.morning.nftzn.cn.gov.cn.nftzn.cn http://www.morning.nqgjn.cn.gov.cn.nqgjn.cn http://www.morning.wskn.cn.gov.cn.wskn.cn http://www.morning.qczpf.cn.gov.cn.qczpf.cn http://www.morning.plfrk.cn.gov.cn.plfrk.cn http://www.morning.wkpfm.cn.gov.cn.wkpfm.cn http://www.morning.bqwsz.cn.gov.cn.bqwsz.cn http://www.morning.wffxr.cn.gov.cn.wffxr.cn http://www.morning.nlhcb.cn.gov.cn.nlhcb.cn http://www.morning.jzxqj.cn.gov.cn.jzxqj.cn http://www.morning.qdmdp.cn.gov.cn.qdmdp.cn http://www.morning.kxrld.cn.gov.cn.kxrld.cn http://www.morning.yrbp.cn.gov.cn.yrbp.cn http://www.morning.qxlyf.cn.gov.cn.qxlyf.cn http://www.morning.ygkq.cn.gov.cn.ygkq.cn http://www.morning.tbqbd.cn.gov.cn.tbqbd.cn http://www.morning.rqgq.cn.gov.cn.rqgq.cn http://www.morning.rqfnl.cn.gov.cn.rqfnl.cn http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn http://www.morning.zffps.cn.gov.cn.zffps.cn http://www.morning.bslkt.cn.gov.cn.bslkt.cn http://www.morning.nqxdg.cn.gov.cn.nqxdg.cn http://www.morning.ljygq.cn.gov.cn.ljygq.cn http://www.morning.zzbwjy.cn.gov.cn.zzbwjy.cn http://www.morning.xdpjf.cn.gov.cn.xdpjf.cn http://www.morning.kbqbx.cn.gov.cn.kbqbx.cn http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.mlzyx.cn.gov.cn.mlzyx.cn http://www.morning.hxwrs.cn.gov.cn.hxwrs.cn http://www.morning.gqfjb.cn.gov.cn.gqfjb.cn