• 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——请求处理器;
      完整源代码请见: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. 參考:

  • 相关阅读:
    handsontable-developer guide-data binding,data sources
    Android版Web服务器实现(三)HTTP响应
    Android版Web服务器实现(二)使用服务来监听HTTP请求
    HTTP协议详解(很详细)
    Android版Web服务器实现(一)HTTP协议请求头解析
    HTTP协议中Content-Length的详细解读。
    Android基于SwiFTP开源库的FTP实现(FTP匿名登录)
    Android之查看Wifi密码
    道德经与抽象、接口及框架
    JAVA中利用JNI与VS2012实现C/C++的DLL调用
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4195040.html
Copyright © 2020-2023  润新知