In this lesson we create a new Express web server app for handling file uploads and persisting them to the filesystem. We will walk through using the express-fileupload middleware module from npm to process file uploads, and then use the Express static middleware to serve the uploaded file as a static asset.

const path = require('path');
const express = require('express');
const fileUpload = require('express-fileupload');
const app = express(); app.use(fileUpload());
app.use('/uploads', express.static(path.join(__dirname, 'uploads'))); app.get('/', (req, res) => {
res.send(`
<form action="/upload" enctype="multipart/form-data" method="post">
<input type="file" name="foo" /><br /><br />
<input type="submit" value="Upload" />
</form>
`);
}); app.post('/upload', (req, res) => {
if (!req.files) return res.status(400).send('No files were uploaded!'); const { foo } = req.files;
const uploadTo = `uploads/${foo.name}`; foo.mv(uploadTo, (err) => {
if (err) return res.status(500).send(err); res.send(`File uploaded to <a href="${uploadTo}">${uploadTo}</a>`);
});
}); app.listen(8080, () => {
console.log('Server listening on port 8080!');
});

[Express] Upload Files with Express的更多相关文章

  1. [Node.js] Serve Static Files with Express

    In this lesson we will find out how to serve static assets (images, css, stylesheets, etc.) with Exp ...

  2. Upload Files To FTP in Oracle Forms D2k

    Upload Files To FTP in Oracle Forms D2k Use following procedure to upload files to Ftp.   PROCEDURE ...

  3. html5 & upload files

    html5 & upload files https://www.sitepoint.com/html5-ajax-file-upload/ https://www.webcodegeeks. ...

  4. How To Use XDOLoader to Manage, Download and Upload Files? (文档 ID 469585.1)

    Applies to: BI Publisher (formerly XML Publisher) - Version 5.6.3 to 5.6.3 [Release 5] Information  ...

  5. How To Use XDOLoader to Manage, Download and Upload Files? (DOC ID 469585.1)

    In this Document Goal Fix     Downloading Files   Uploading Files References Applies to: BI Publishe ...

  6. nodejs安装express以后,使用express显示不是内部或外部命令

    1.问题描述 在命令窗口通过npm install -g express 安装express以后,通过express -e express新建工程失败,提示express不是内部或外部命令 2.解决方 ...

  7. nodejs 实践:express 最佳实践(三) express 解析

    nodejs 实践:express 最佳实践(三) express 解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs 的基础不稳固, ...

  8. nodejs 实践:express 最佳实践(六) express 自省获得所有的路由

    nodejs 实践:express 最佳实践(六) express 自省获得所有的路由 某些情况下,你需要知道你的应用有多少路由,这在 express 中没有方法可以.因此我这边曲线了一下,做成了一个 ...

  9. HTTPWebrequest上传文件--Upload files with HTTPWebrequest (multipart/form-data)

    使用HTTPWebrequest上传文件遇到问题,可以参考Upload files with HTTPWebrequest (multipart/form-data)来解决 https://stack ...

随机推荐

  1. Zookeeper入门-Java版本HelloWorld例子

    上一篇介绍了,Zookeeper的基本概念,怎么启动,怎么解决可能遇到的几个问题.本篇,根据网上代码,整理了一个例子,Zookeeper的HelloWorld. 下面这个代码,还是比较简单的,核心类就 ...

  2. 【Henu ACM Round#20 E】Star

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找规律. 1,13,37.... 6n(n-1) + 1 [代码] #include <bits/stdc++.h> # ...

  3. JAVA类库LinkList的基本实现

    写完调试了好久,边界不优点理,具体的请看JDK类库,下面仅仅是基本实现: import java.util.Iterator; /** * 类名:MyLinkedList 说明:LinkedList的 ...

  4. oh-my-zsh upgrade problem

    Oh-My-ZSH upgrade issue with bad substitution message   Any problem with automatic Oh-My-Zsh upgrade ...

  5. CentOS下安装C/C++开发工具包的最佳方式

    如果你使用的是 Fedora, Red Hat, CentOS, 或者 Scientific Linux 系统,使用下面的命令安装GNU的C/C++开发包和编译器. # yum groupinstal ...

  6. WebApi 参数请求

    收藏来源于:http://www.cnblogs.com/babycool/p/3922738.html 路由配置到id post多个参数 ➕前缀 FromBody 参数为实体 对于一般前台页面发起的 ...

  7. 电脑无法上网,DHCP客户端不能正确获取IP地址

    问题特征:DHCP服务器更新[保留]配置信息后,给一客户端绑定了新的IP地址;但客户端IP地址并未正确更新; 处理: 一.检查DHCP服务器配置; 1.MAC地址.IP地址均正确;并已“添加到筛选器” ...

  8. HDU 4431 Mahjong 模拟

    http://acm.hdu.edu.cn/showproblem.php?pid=4431 不能说是水题了,具体实现还是很恶心的...几乎优化到哭但是DFS(还加了几个剪枝)还是不行...搜索一直T ...

  9. MySQL Pool

    创建连接池 function SqlPool() { this.flag = true;//是否连接过 this.pool = mysql.createPool({ host : 'localhost ...

  10. Linux学习总结(1)——Linux命令大全完整版

    Linux命令大全完整版 目    录I 1. linux系统管理命令1 adduser1 chfn(change finger information)1 chsh(change shell)1 d ...