Node. js form -- formidable
Node needs to use the formidable package to process form requests. To install the formidable package, run the following command: npm install formidable to install package in two ways: local directory and global directory. Run the npm install xxx-g command to download and install the module to the global directory. You can set the global directory through npm config set prefix "directory path. Use npm config get prefix to obtain the directory currently set. The npm install xxx command downloads the module to the directory where the current command line is located. The package installation path is described on the Internet as above, but when I do not perform global installation, formidable is installed under the user's home directory/home/user name/node_modules, instead of installing it in the directory where the command line is located, the ubuntu version I use is 14.04. The reason is not clear. Which of the following is the best directory and global directory in the current directory? I think they have their own advantages. I will not go into details here. After formidable is installed, a standard node project should have an index. js file to define how to handle the request from the browser. The index. js code is as follows:
Var http = require ("http"); // obtain the http object var url = require ("url "); // obtain the url object // http and url are all built-in modules of the system. The following requestHandlers is a module written manually, which corresponds to requestHandlers in the current directory. js file var requestHandlers = require (". /requestHandlers "); // onRequest function, used to process http requests. Different url requests are handled by different functions. function onRequest (request, response) {var pathname = url. parse (request. url ). pathname; // get the requested URL // The handle attribute of requestHandlers is another object, which contains multiple sets of attributes: attribute value, attribute name corresponds to uri, attribute value pair Processing functions. For details, 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 accessed does not exist! Visit the <a href = '/'> homepage </a> "); response. end () ;}} var server = http. createServer (onRequest); // The onRequest function is used as the parameter of createServer. In this way, each time the browser sends a request, the server calls this function. listen (8888); // the preceding two sentences can be combined into http. createServer (onRequest ). listen (0, 8888 );
The above code can almost become the index template of most node projects, and the changes will not be great. If you add a function to the application, you only need to change the later requestHandlers. js file. In the past, the teacher mentioned a basic node application divided into index. js, server. js, router. js and requestHandlers. js, while server. js and router. javaScript code never needs to be changed and can be applied to any project. Later I found that the first three parts can be integrated together, so I figured out the index above. js, and later I found that after integration, I had a better understanding. The following is the code for requestHandlers. js:
Var handle ={}; var formidable = require ("formidable"); handle ["/"] = start; handle ["/start"] = start; handle ["/upload"] = upload; function start (request, response) {var body = '
The preceding example shows how to extract information from a form after a form is submitted. Handle is an object that contains multiple sets of attributes and attribute values. The attribute name corresponds to the url, and the attribute value corresponds to the processing function. That is, the request url corresponds to the function one by one. Each request sent by the browser, the server analyzes the request url and processes the request using the function corresponding to the value of the attribute named this url in handle. For example, if the browser sends a request whose uri is/upload and the server finds handler, handler ["/upload"] = upload, it calls the upload function to process the request. All attributes and functions in the module that may be called by external files must be exported using the exports command, such as the end part of requestHandlers. js. Therefore, if you add a link or form request/xxx to the page when adding a function, you usually need. modify the js file in three steps: 1. Add handler ["/xxx"] = xxx; 2. Add function xxx (request, response ){...}; 3. Add exports. xxx = xxx;