Getting started with Node. js-how to upload a node. js File
Author: zhanhailiang Date:
This article describes how to use Node. js to upload files.
1. initialize the project information: npm init
[root@~/wade/nodejs/nodejs-upload-image-demo]# npm initThis 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 fieldsand exactly what they do. Use `npm install <pkg> --save` afterwards to install a package andsave 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 imageentry point: (index.js) test command: git repository: (https://github.com/billfeller/nodejs-upload-image-demo.git) keywords: author: billfellerlicense: (ISC) MITAbout 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. Modify package. json and configure the startup command:
"scripts": { "start": "node index.js" },
3. install the dependency module: npm install formidable-save
4. function implementation:
- Index. js-Unified entry, including request processor registration;
- Server. js-server module, including server creation, request listening, and route policy execution;
- Router. js-routing policy, which routes requests to the corresponding processor based on the request url;
- RequestHandler. js -- request processor;
Complete 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. Access and upload images through a browser:
6. refer: