作者: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. Jquery中使用setInterval和setTimeout 容易犯的低级错误

    直接在ready中调用其他方法,会提示缺少对象的错误,解决方法如下: 方法1. 应用jQuery的扩展可以解决这个问题. $(document).ready(function(){ $.extend( ...

  2. Div 滚动栏滚动到指定的位置

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. [ACM] HDU 3395 Special Fish (最大重量二分图匹配,KM算法)

    Special Fish Problem Description There is a kind of special fish in the East Lake where is closed to ...

  4. hdu1964之插头DP求最优值

    Pipes Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  5. Agile/CMMI/Scrum

    Agile/CMMI/Scrum 一.背景介绍 在朋友(aehyok)的建议下,初步去了解Visual Studio Online,简称VS Online(即原来的 Team Foundation S ...

  6. C#并行编程

    C#并行编程中的Parallel.Invoke 一.基础知识 并行编程:并行编程是指软件开发的代码,它能在同一时间执行多个计算任务,提高执行效率和性能一种编程方式,属于多线程编程范畴.所以我们在设计过 ...

  7. 每天进步一点点之SQL 获取表中某个时间字段离当前时间最近的几条

    实际中用到的SQL: select * from (select top 3 Id, case when startSignup>GETDATE() then '敬请期待' when (star ...

  8. CAS Spring Security 3 整合配置(转)

    一般来说, Web 应用的安全性包括用户认证( Authentication )和用户授权( Authorization )两个部分.用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否 ...

  9. 《数字图像处理原理与实践(MATLAB文本)》书代码Part7

    这篇文章是<数字图像处理原理与实践(MATLAB文本)>一本书的代码系列Part7(由于调整先前宣布订单,请读者注意分页程序,而不仅仅是基于标题数的一系列文章),第一本书特色186经225 ...

  10. [Ruby]How to create singleton class ?

    Singleton is one design pattern in the software engineering. Ruby has its own special feature to dec ...