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——请求处理器;
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 ...
随机推荐
- [Windwos Phone 8]多个按钮的共用事件
原文:[Windwos Phone 8]多个按钮的共用事件 前言 ------------------------------------------------------------------- ...
- 动态Lambda进阶一
直接上代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...
- [LeetCode]Word Ladder 最短距离字符串转换 (Dijkstra)
要求最短距离.采纳dijkstra查找节点之间的最短路径. 当心:假设是一个枚举字典22是否元素可以,如果转换,暂停. 提高:每串,带您历数它的字符值事件,对于的长度n一个字符串枚举n*26次要. 设 ...
- js匀速运动停止条件
匀速运动,怎么让它到达指定位置时停止呢? 原理: 1,物体和目标的差值距离小于等于速度时,即停止 2,接着让物体移动位置等于目标位置 示例:匀速运动停止 html部分 <input type=& ...
- how to fix the cwm/miui recovery status 2 error
Frequent issues: --------------------- If you report an issue without a log and/or without details, ...
- Android自带样式
Android系统自带样式: android:theme="@android:style/Theme.Dialog" 将一个Activity显示为对话框模式 android:the ...
- 【Unity技能】做一个简单的NPC
1. 写在前面 前几天我看到cgcookie一个教程.学习了下怎么依据已有人物模型制作一个仿版的NPC人物.感觉挺好玩的,整理一下放到博客里! 先看一下教程里面的终于效果. 是不是非常像个幽灵~ 以下 ...
- [LeetCode257]Binary Tree Paths
题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...
- MongoDB最新版本3.2.9下载地址
https://downloads.mongodb.com/win32/mongodb-win32-x86_64-enterprise-windows-64-3.2.9.zip?_ga=1.22538 ...
- 原来Github上的README.md文件这么有意思——Markdown语言详解
转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 之前一直在使用github,也在上面分享了不少的项目和Demo,每次创建新项目的时候,使用的都是默认的REA ...