作者: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新手教程——怎样实现文件上传功能的更多相关文章

  1. node.js使用express框架进行文件上传

    关于node.js使用express框架进行文件上传,主要来自于最近对Settings-Sync插件做的研究.目前的研究算是取得的比较好的进展.Settings-Sync中通过快捷键上传文件,其实主要 ...

  2. 使用Node.js的Express框架进行文件上传

    我们先创建一个Express项目,要使用文件上传的功能还需要下载multer模块. npm install --save multer 下面我们在public文件夹下创建upload.html,内容如 ...

  3. node.js中 express + multer 处理文件上传

    multer中间件,可以很方便的结合express处理用户表单上传的文件. 一.安装multer npm install multer 二.处理单个文件上传 const express = requi ...

  4. 原生JS和jQuery版实现文件上传功能

    <!doctype html> <html lang="zh"> <head> <meta charset="utf-8&quo ...

  5. Node.js 博客实例(三)添加文件上传功能

    原教程 https://github.com/nswbmw/N-blog/wiki/_pages的第三章 上传文件眼下有三种方法: 使用 Express 自带的文件上传功能,不涉及数据库 使用 For ...

  6. Bootstrap fileinput.js,最好用的文件上传组件

    本篇介绍如何使用bootstrap fileinput.js(最好用的文件上传组件)来进行图片的展示,上传,包括springMVC后端文件保存. 一.demo   二.插件引入 <link ty ...

  7. js 实现 input type="file" 文件上传示例代码

    在开发中,文件上传必不可少但是它长得又丑.浏览的字样不能换,一般会让其隐藏点其他的标签(图片等)来时实现选择文件上传功能 在开发中,文件上传必不可少,<input type="file ...

  8. [置顶] js 实现 <input type="file" /> 文件上传

    在开发中,文件上传必不可少,<input type="file" /> 是常用的上传标签,但是它长得又丑.浏览的字样不能换,我们一般会用让,<input type ...

  9. 黑马lavarel教程---7、文件上传

    黑马lavarel教程---7.文件上传 一.总结 一句话总结: 在laravel里面实现文件的上传是很简单的,压根不用引入第三方的类库,作者把上传作为一个简单的http请求看待的. 1.在lavar ...

随机推荐

  1. 【游戏】2048及各种变种大集合汇总【更新ing~新版Floppy2048 - 恒星聚变版 - 恶搞改数据】

    threes - 鼻祖 手机版:http://asherv.com/threes/ js版:http://threesjs.com/ 2048 - 原版 http://gabrielecirulli. ...

  2. javascript实现倒计时-------Day28

    先来两张图片,看一看今天写什么: 看到图片右上角是什么了么看到图片以下是什么了么 相信这个大家都不会陌生吧.那些生活中等着秒杀,等着抢小米人们,焦躁等待的你曾一秒一秒的盯着它看么,我不知道答案,可我知 ...

  3. JSP中的include有哪些?有什么差别?

    JSP中的include有哪些?有什么差别? 1.JSP中的include有哪些 (1)<%@include file="" %> (2)<jsp:include ...

  4. hdu4288 Coder(段树+分离)

    主题链接: huangjing 题意: 题目中给了三个操作 1:add x 就是把x插进去  2:delete x 就是把x删除 3:sum 就是求下标%5=3的元素的和. 另一个条件是插入和删除最后 ...

  5. android 常见的解决(mdpi、hdpi 、xhdpi、xxhdpi )屏幕调整

    查询到执行的system service后,就能够在dumpsys后面加上service的名字,查看指定的service信息. adb shell dumpsys activity adb shell ...

  6. C++使用对象指针

    //定义结构 Box.h: #ifndef BOX_H #define BOX_H struct Box{ double length; double width; double height; do ...

  7. OC动态创建的问题变量数组.有数组,在阵列13要素,第一个数据包阵列,每3元素为一组,分成若干组,这些数据包的统一管理。最后,一个数组.(要动态地创建一个数组).两种方法

    <span style="font-size:24px;">//////第一种方法 //        NSMutableArray *arr = [NSMutable ...

  8. LR杂记 - loadrunner各项指标结果分析

    Transactions (用户事务分析) 用户事务分析是站在用户角度进行的基础性能分析. 1 . Transation Sunmmary (事务综述) 对事务进行综合分析是性能分析的第一步,通过分析 ...

  9. 【C语言探索之旅】 第二部分第四课:字符串

    内容简介 1.课程大纲 2.第二部分第四课: 字符串 3.第二部分第五课预告: 预处理 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三个游戏. ...

  10. 了解HTML5和“她”的 API (一)

    简化了文档声明.字符集 //声明 <!doctype html> //字符 <meta charset="utf-8">  引入了新的标签元素 Select ...