node. js form--formidable/////z

Source: Internet
Author: User

node. js form--formidable
Node handles form requests and needs to use the formidable package. The command to install the formidable package is as follows: NPM install formidable there are two paths for installing packages, one is a local directory and one is a global directory. The NPM install XXX-G command installs the module download to the global catalog. The global catalog can be set through the NPM config set prefix "directory path". Use NPM config get prefix to get the current set of directories. NPM Install XXX, you download the module to the directory where the current command line is located. Description of the package installation path The Web is like the above, but when I do not perform a global installation, formidable is installed under the user's home directory/home/user name/node_modules, not the directory where the command line is located. The Ubuntu version I used is 14.04, the reason is not very clear. Installed in the local directory and the global directory which is good, I think each has an advantage. This is not to be discussed here. After installing formidable, a standard node project should have a index.js file that defines how the Request,index.js code from the browser is handled as follows:
var http = require ("http");//Gets the HTTP object var url = require ("url");//Gets the URL object//http, the URL is the system comes with the module, The following requesthandlers is the module we have written manually, corresponding to the current directory of requesthandlers.js file var requesthandlers = require ("./requesthandlers");// The ONrequest function is used to process HTTP requests, and different URL requests are assigned to different functions for processing function onrequest (request, Response) {var pathname = Url.parse ( Request.url). pathname;//Gets the URL of the requested//requesthandlers the handle property is another object that contains multiple sets of properties: Property value, property name corresponding to URI, property value corresponding to the handler function, See Requesthandlers.js if (typeof requesthandlers.handle[pathname] = = = "function") Requesthandlers.handle[pathname]    (Request, response);        else {console.log ("No request handler found for" + pathname);        Response.writehead (404, {"Content-type": "Text/html;charset=utf-8"}); Response.Write ("The page you visited does not exist!")        Visit <a href= '/' > Home </a> ');    Response.End (); }}var Server = Http.createserver (onrequest);//function ONrequest as a parameter of createserver so that every request is sent by the browser, The function in the server will be called once Server.listen (8888);//The above two sentences can be written http.createserver (onrequest). Listen (8888);

The above code can almost become the majority of the Node Project index template, there is no big change, if you add functionality to the application, only need to change the following requesthandlers.js file. One of the basic node applications that teachers used to teach at school was divided into Index.js, Server.js, Router.js and requesthandlers.js four parts, and Server.js and Router.js code is never need to change, can be applied to any project, then I found that the first three parts can be integrated together, so I figured out the above index . js, later found that after the integration is actually OK to understand a little. The following requesthandlers.js code is posted:
var handle = {};var formidable = require ("formidable"); handle["/"] = start;handle["/start"] = start;handle["/upload"] = up Load;function Start (Request, response) {var body = ' 
   

It shows an example of extracting information from a form form after a form form is submitted. Where handle is an object that contains multiple sets of attributes and attribute values, the property name corresponds to the URL, and the property value corresponds to the handler function, that is, the requested URL corresponds to the function one by one, the browser sends a request each, the server parses the requested URL, The request is processed by using the function that corresponds to the value of the property named the URL in handle. For example, the browser sends a request, the URI is/upload, the server finds the handler, handler["/upload"] = upload, it calls the upload function to process the request. All properties that may be called by external files in the module, functions need to be exported using the exports command, such as the end of Requesthandlers.js. Therefore, if you add a function, the page adds a link or a form such as request/xxx, you usually need to make changes in the Requesthandlers.js file, in three steps: First, add handler["/xxx"] = XXX; Second, add function xxx (request, response) {...}; Third, increase exports.xxx = xxx;

node. js form--formidable/////z

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.