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. python常用函数库收集。

    学习过Python都知道python中有很多库.python本身就是万能胶水,众多强大的库/模块正是它的优势. 收集一些Python常用的函数库,方便大家选择要学习的库,也方便自己学习收集,熟悉运用好 ...

  2. zabbix4.0 使用nginx前端安装

    注:环境需求:centos7 1.安装阿里云yum源: rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/4.1/rhel/7/x86_64/zabb ...

  3. 一个最不可思议的MySQL死锁分析

    1    死锁问题背景    1 1.1    一个不可思议的死锁    1 1.1.1    初步分析    3 1.2    如何阅读死锁日志    3 2    死锁原因深入剖析    4 2. ...

  4. 用C3P0建立server与数据库的连接

    1:在MyEclipse建立 Web Service Project 2:在project中建立servlets包 3:在包中新建Servlet文件(採用new Servlet方法可省去配置web.x ...

  5. HDU 5391-Zball in Tina Town(数论)

    题目地址:pid=5391">HDU 5391 题意: Tina Town 是一个善良友好的地方,这里的每个人都互相关心.Tina有一个球,它的名字叫zball. zball非常奇妙, ...

  6. c++开源爬虫-Larbin简单介绍

    原文地址:http://leihuang.net/2014/06/16/Larbin-Introduction/ 由于近期学校实训.做的是一个搜索相关的项目,而且是c++的一个项目.所以就想到了lar ...

  7. Yocto tips (10): Yocto hellworld 加入一个软件包

    Yocto中一个软件包是放在bb文件里的,然后非常多的bb文件集成一个recipe(配方),然后很多的recipe又组成一个meta layer.因此,要加入一个包事实上就是在recipe以下加入一个 ...

  8. Git的日常处理流程

    前提 本地有2个分支,一个是master,还有一个是local master 默认追踪origin/master local 通过git branch -u origin/master来映射 开发的时 ...

  9. Fedora27 安装Adobe Flash Player PPAPI与NPAPI实现Firefox和Chromium视频播放

    一.Adobe Flash Player PPAPI与NPAPI有什么区别我们在打开网页视频时有时会弹出没有安装Flash插件的提示,此时就无法观看视频.Adobe Flash Player是浏览器显 ...

  10. 【VC++学习笔记五】SDI|MDI的全屏显示

    一.Mainframe中添加一个记录是否全屏状态的变量BOOL m_bFullScreen. 二.工具栏添加一个按钮,进行全屏的操作,响应事件函数写在Mainframe中. 三.在响应函数中,添加如下 ...