1. 作者:zhanhailiang 日期:2014-11-16

本文将介绍怎样使用Node.js实现文件上传功能。

1. 初始化项目信息:npm init

  1. [root@~/wade/nodejs/nodejs-upload-image-demo]# npm init
  2. This utility will walk you through creating a package.json file.
  3. It only covers the most common items, and tries to guess sane defaults.
  4.  
  5. See `npm help json` for definitive documentation on these fields
  6. and exactly what they do.
  7.  
  8. Use `npm install <pkg> --save` afterwards to install a package and
  9. save it as a dependency in the package.json file.
  10.  
  11. Press ^C at any time to quit.
  12. name: (nodejs-upload-image-demo)
  13. version: (1.0.0)
  14. description: how to use node.js to upload an image
  15. entry point: (index.js)
  16. test command:
  17. git repository: (https://github.com/billfeller/nodejs-upload-image-demo.git)
  18. keywords:
  19. author: billfeller
  20. license: (ISC) MIT
  21. About to write to /root/wade/nodejs/nodejs-upload-image-demo/package.json:
  22.  
  23. {
  24. "name": "nodejs-upload-image-demo",
  25. "version": "1.0.0",
  26. "description": "how to use node.js to upload an image",
  27. "main": "index.js",
  28. "scripts": {
  29. "test": "echo \"Error: no test specified\" && exit 1"
  30. },
  31. "repository": {
  32. "type": "git",
  33. "url": "https://github.com/billfeller/nodejs-upload-image-demo.git"
  34. },
  35. "author": "billfeller",
  36. "license": "MIT",
  37. "bugs": {
  38. "url": "https://github.com/billfeller/nodejs-upload-image-demo/issues"
  39. },
  40. "homepage": "https://github.com/billfeller/nodejs-upload-image-demo"
  41. }
  42.  
  43.  
  44. Is this ok? (yes) yes

2. 改动package.json,配置启动命令:

  1. "scripts": {
  2. "start": "node index.js"
  3. },

3. 安装依赖模块:npm install formidable –save

4. 功能实现:

  • index.js——统一入口,包含请求处理器注冊;
  • server.js——server模块,包含server创建,监听请求,运行路由策略;
  • router.js——路由策略,依据请求url路由到对应的处理器进行请求处理;
  • requestHandler.js——请求处理器;
  1. 完整源代码请见:https://github.com/billfeller/nodejs-upload-image-demo

5. 启动server:

  1. [root@~/wade/nodejs/nodejs-upload-image-demo]# npm start
  2.  
  3. > nodejs-upload-image-demo@1.0.0 start /root/wade/nodejs/nodejs-upload-image-demo
  4. > node index.js
  5.  
  6. 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. [Windwos Phone 8]多个按钮的共用事件

    原文:[Windwos Phone 8]多个按钮的共用事件 前言 ------------------------------------------------------------------- ...

  2. 动态Lambda进阶一

    直接上代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  3. [LeetCode]Word Ladder 最短距离字符串转换 (Dijkstra)

    要求最短距离.采纳dijkstra查找节点之间的最短路径. 当心:假设是一个枚举字典22是否元素可以,如果转换,暂停. 提高:每串,带您历数它的字符值事件,对于的长度n一个字符串枚举n*26次要. 设 ...

  4. js匀速运动停止条件

    匀速运动,怎么让它到达指定位置时停止呢? 原理: 1,物体和目标的差值距离小于等于速度时,即停止 2,接着让物体移动位置等于目标位置 示例:匀速运动停止 html部分 <input type=& ...

  5. how to fix the cwm/miui recovery status 2 error

    Frequent issues: --------------------- If you report an issue without a log and/or without details, ...

  6. Android自带样式

    Android系统自带样式: android:theme="@android:style/Theme.Dialog" 将一个Activity显示为对话框模式 android:the ...

  7. 【Unity技能】做一个简单的NPC

    1. 写在前面 前几天我看到cgcookie一个教程.学习了下怎么依据已有人物模型制作一个仿版的NPC人物.感觉挺好玩的,整理一下放到博客里! 先看一下教程里面的终于效果. 是不是非常像个幽灵~ 以下 ...

  8. [LeetCode257]Binary Tree Paths

    题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...

  9. MongoDB最新版本3.2.9下载地址

    https://downloads.mongodb.com/win32/mongodb-win32-x86_64-enterprise-windows-64-3.2.9.zip?_ga=1.22538 ...

  10. 原来Github上的README.md文件这么有意思——Markdown语言详解

    转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 之前一直在使用github,也在上面分享了不少的项目和Demo,每次创建新项目的时候,使用的都是默认的REA ...