Node.js新手教程——怎样实现文件上传功能
作者:zhanhailiang 日期:2014-11-16
本文将介绍怎样使用Node.js实现文件上传功能。
1. 初始化项目信息:npm init
[root@~/wade/nodejs/nodejs-upload-image-demo]# npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
name: (nodejs-upload-image-demo)
version: (1.0.0)
description: how to use node.js to upload an image
entry point: (index.js)
test command:
git repository: (https://github.com/billfeller/nodejs-upload-image-demo.git)
keywords:
author: billfeller
license: (ISC) MIT
About to write to /root/wade/nodejs/nodejs-upload-image-demo/package.json:
{
"name": "nodejs-upload-image-demo",
"version": "1.0.0",
"description": "how to use node.js to upload an image",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/billfeller/nodejs-upload-image-demo.git"
},
"author": "billfeller",
"license": "MIT",
"bugs": {
"url": "https://github.com/billfeller/nodejs-upload-image-demo/issues"
},
"homepage": "https://github.com/billfeller/nodejs-upload-image-demo"
}
Is this ok? (yes) yes
2. 改动package.json,配置启动命令:
"scripts": {
"start": "node index.js"
},
3. 安装依赖模块:npm install formidable –save
4. 功能实现:
- index.js——统一入口,包含请求处理器注冊;
- server.js——server模块,包含server创建,监听请求,运行路由策略;
- router.js——路由策略,依据请求url路由到对应的处理器进行请求处理;
- requestHandler.js——请求处理器;
完整源代码请见:https://github.com/billfeller/nodejs-upload-image-demo
5. 启动server:
[root@~/wade/nodejs/nodejs-upload-image-demo]# npm start
> nodejs-upload-image-demo@1.0.0 start /root/wade/nodejs/nodejs-upload-image-demo
> node index.js
server is starting
6. 通过浏览器訪问并上传图片:
6. 參考:
Node.js新手教程——怎样实现文件上传功能的更多相关文章
- node.js使用express框架进行文件上传
关于node.js使用express框架进行文件上传,主要来自于最近对Settings-Sync插件做的研究.目前的研究算是取得的比较好的进展.Settings-Sync中通过快捷键上传文件,其实主要 ...
- 使用Node.js的Express框架进行文件上传
我们先创建一个Express项目,要使用文件上传的功能还需要下载multer模块. npm install --save multer 下面我们在public文件夹下创建upload.html,内容如 ...
- node.js中 express + multer 处理文件上传
multer中间件,可以很方便的结合express处理用户表单上传的文件. 一.安装multer npm install multer 二.处理单个文件上传 const express = requi ...
- 原生JS和jQuery版实现文件上传功能
<!doctype html> <html lang="zh"> <head> <meta charset="utf-8&quo ...
- Node.js 博客实例(三)添加文件上传功能
原教程 https://github.com/nswbmw/N-blog/wiki/_pages的第三章 上传文件眼下有三种方法: 使用 Express 自带的文件上传功能,不涉及数据库 使用 For ...
- Bootstrap fileinput.js,最好用的文件上传组件
本篇介绍如何使用bootstrap fileinput.js(最好用的文件上传组件)来进行图片的展示,上传,包括springMVC后端文件保存. 一.demo 二.插件引入 <link ty ...
- js 实现 input type="file" 文件上传示例代码
在开发中,文件上传必不可少但是它长得又丑.浏览的字样不能换,一般会让其隐藏点其他的标签(图片等)来时实现选择文件上传功能 在开发中,文件上传必不可少,<input type="file ...
- [置顶] js 实现 <input type="file" /> 文件上传
在开发中,文件上传必不可少,<input type="file" /> 是常用的上传标签,但是它长得又丑.浏览的字样不能换,我们一般会用让,<input type ...
- 黑马lavarel教程---7、文件上传
黑马lavarel教程---7.文件上传 一.总结 一句话总结: 在laravel里面实现文件的上传是很简单的,压根不用引入第三方的类库,作者把上传作为一个简单的http请求看待的. 1.在lavar ...
随机推荐
- js缓冲运动
缓冲运动 现象:逐渐变慢,最后停止 原理:距离越远,速度越大 速度的计算方式: 1,速度由距离决定 2,速度=(目标值-当前值)/缩放系数 说明:速度为正负数时,也决定了物体移动的方向 示例:div缓 ...
- 使用 WPF 实现所见即所得HTML编辑器
Introduction In this tip, you will learn the use of WPF webbrowser control and the use of the librar ...
- SRM 628 D1L3:DoraemonPuzzleGame,math,后市展望,dp
称号:c=problem_statement&pm=13283&rd=16009">http://community.topcoder.com/stat?c=probl ...
- jQuery 焦点图,图像文件js档
jQuery 焦点图,图片文件在js文件里 演示 XML/HTML Code <div id="photo_container"></div> JavaSc ...
- DHCP Option 60 认识
原文地址: http://blog.163.com/chenqioulin_1983/blog/static/83216232010109104430251/ 首先还是看看RFC咋说的吧.DHCP ...
- 【Linux探索之旅】第二部分第二课:命令行,世界尽在掌握
内容简介 1.第二部分第二课:命令行,世界尽在掌握 2.第二部分第三课预告:文件和目录,组织不会亏待你 命令行,世界尽在掌握 今天的标题是不是有点霸气侧漏呢? 读者:“小编,你为什么每次都要起这么非主 ...
- Stack-based buffer overflow in acdb audio driver (CVE-2013-2597)
/* 本文章由 莫灰灰 编写,转载请注明出处. 作者:莫灰灰 邮箱: minzhenfei@163.com */ 1. 漏洞描写叙述 音频驱动acdb提供了一个ioctl的系统接口让应用层调用, ...
- HDU 1711 Number Sequence(算法验证)
该怎么做.每一个人的人生都应该自己掌握.你给不了别人一切.你也不懂别人的忧伤. 微笑不代表快乐.哭泣不一定悲伤 不努力怎么让关心你的人幸福.不努力怎么让看不起你的人绝望. 我用生命在奋斗--lx_Zz ...
- CSS十问——好奇心+刨根问底=CSSer (转)
最近有时间,想把酝酿的几篇博客都写出来,今天前端小学生带着10个问题,跟大家分享一下学习CSS的一些体会,我觉得想学好CSS,必须保持一颗好奇心和刨根问底的劲头,而不是复制粘贴,得过且过.本人能力有限 ...
- IP多播(组播)
IP多播是实现数据一对多通信的模式.从一个源点传送到多个目的地,数据仅仅拷贝一份.这里说的数据仅仅拷贝一份,是指在每一条须要它的两个点之间,数据仅仅有一份.例如以下图为<计算机网络>(谢希 ...