Zhanhailiang Date: 2014-11-16
This article describes how to use node. js to implement file upload functionality.
1. Initializing project information: NPM Init
[Root@~/Wade/Nodejs/Nodejs-upload-image-demo]# NPM InitThis utility would walk you through creating a Package.json file. It is covers the most common items, and tries to guess sane defaults. See`Npm HelpJson` forDefinitive documentation on these fieldsand exactly what they do. Use`NpmInstall <Pkg>--save`Afterwards toInstallA package Andsave it asA dependencyinchThe Package.json file. Press ^c at any TimeTo Quit.name:(Nodejs-upload-image-demo)Version(1.0.0)Description:how to use node. js to upload a imageentry point:(Index.js) TestCommandgitRepository(Https//Github.com/Billfeller/Nodejs-upload-image-demo.git)Keywords:author:billfellerlicense:(Isc)Mitabout toWriteTo/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. Change Package.json, configure the start command:
"Scripts": { "start": "Node Index.js"},
3. Install the Dependent module: NPM install Formidable–save
4. Function Realization:
- index.js--Unified portal with request processor register;
- server.js--server module, which contains server creation, listens for requests, runs routing policies,
- router.js--routing policy, routed to the corresponding processor for request processing according to the request URL,
- requesthandler.js--request processor;
Full source code See: Https://github.com/billfeller/nodejs-upload-image-demo
5. Start the 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. Browse through the browser and upload the image:
6. References:
- Node Getting Started
- Nodebeginner
node. js Novice Tutorial--How to implement file upload function