作者: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. Java Web整合开发(79) -- Struts 2

    一. Struts 2.x 概述 不继承任何类的Action Struts 2的Action并不一定要实现Action接口或者继承ActionSupport,任何POJO都可以做Action,只要这个 ...

  2. C#改动文件或目录的权限,为指定用户、用户组加入全然控制权限

    C#改动文件或文件夹的权限,为指定用户.用户组加入全然控制权限 //给Excel文件加入"Everyone,Users"用户组的全然控制权限 FileInfo fi = new F ...

  3. Linux - SVN下载项目

    SVN下载项目 本文地址:http://blog.csdn.net/caroline_wendy 使用SVN.在Git上下载项目. $ mkdir chunyu_trunk //创建目录 $ ls / ...

  4. 怎么样cocos2d-x正在使用ECS(实体-包裹-制)建筑方法来开发一款游戏?

    简介 在我的博客,我翻译的几篇文章ECS文章.这些文章都是从Game Development站点.假设你对这个架构方式还不是非常了解的话.欢迎阅读理解 组件-实体-系统和实现 组件-实体-系统. 我发 ...

  5. [Unity3D]Unity3D游戏开发Lua随着游戏的债券(于)

    ---------------------------------------------------------------------------------------------------- ...

  6. [Android]Can&#39;t create handler inside thread that has not called Looper.prepare()

    更新是由于在新的线程来打开UI只有一个错误.子线程更新主线程UI需要使用Handler. 还有比如今天出现以下错误.码,如以下: send.setOnClickListener(new OnClick ...

  7. Android开发经验—不要指望类finalize干活的方法做你想要什么

    之所以专门写了一篇文章finalize方法博客,这是通过在坑的方法引起的.一个读写jni当数据类.我在课堂上finalize该方法被调用来关闭文件和释放内存的方法.频繁调用这个类的时候在JNI里面报异 ...

  8. 如何在 Swift 中优雅地处理 JSON

    阅读目录 在Swift中使用JSON的问题 开始 基础用法 枚举(Enumeration) 下标(Subscripts) 打印 调试与错误处理 后记   因为Swift对于类型有非常严格的控制,它在处 ...

  9. 不一样的是不一样的,我的独家滚动条------Day35

    在您开始建立自己的,感觉应该先录一个概念:内核的浏览器. 兼容性问题之前多次提及,而在平时经常会遇到兼容性问题.原因,就在于它:浏览器内核.这是比較通俗的说法,事实上应该把它描写叙述的专业点:Rend ...

  10. Event Sourcing

    Event Sourcing - ENode(二) 接上篇文章继续 http://www.cnblogs.com/dopeter/p/4899721.html 分布式系统 前篇谈到了我们为何要使用分布 ...