Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Node.js form --formidable     - Oracle Database Delete Delete million or more common method of heap table data (Database)

- Archive log file size and redo log file size relationship (Database)

- Linux under DB2SQL1024N A database connection does not exist. SQLS (Database)

- Memcached installation, configuration and monitoring (Server)

- How MySQL tracking sql statement (Database)

- Introduction and MongoDB CRUD (Database)

- Tab set to four spaces in Vim (Linux)

- Lenovo E431 notebook CentOS system is installed wireless network card driver (Linux)

- Ubuntu installation module Python rq (Linux)

- Docker data container protection the pros and cons (Server)

- CentOS 6.4 compiler installed MySQL 5.6.14 (Database)

- Make full use of the Raspberry Pi SD card space (Linux)

- OpenSIPS offline messaging feature set (Server)

- How to properly set up a Linux swap partition (Linux)

- Disable unnecessary services under Linux (Linux)

- Deploy Mono 4 and Jexus 5.6 on CentOS7 (Server)

- Linux file compression and file system packaged with instructions (Linux)

- Try debugfs restore the deleted files ext3 file system (Linux)

- How to clear the DNS query cache under Linux / Unix / Mac (Linux)

- Exploring the Android Listview display confusion (Programming)

  Node.js form --formidable
  Add Date : 2017-08-31      
  Node Processing the request form, you need to use the formidable package. Installation formidable package command as follows:

npm install formidable

Installation package is divided into two paths, one is a local directory, one is the global directory.

npm install xxx -g command to download and install the module into the global directory.

Global Directory can config set prefix "directory path" set by npm.

To get the current directory set by npm config get prefix.

npm install xxx, sucked module downloaded to the current directory where the command line.

Description Online is like that just said, but I do not perform global installation time on package installation path, formidable is installed in the user's home directory / home / username / node_modules Now, instead of installing the command line directory, I Ubuntu version 14.04 is used, the reason is not clear. Installed in the directory and in the present global catalog in the end which is good, I think have their own advantages. Not go into here.

After installing formidable, a standard node project, there should be a index.js file that defines how the processing request from the browser, index.js code is as follows:

var http = require ( "http"); // Gets http objects
var url = require ( "url"); // get the url Object
// Http, url is the system comes with the module, and the module below requestHandlers we manually written requestHandlers.js file corresponding to the current directory
var requestHandlers = require ( "./ requestHandlers");

// OnRequest functions for handling http requests, different url request to be processed by the different functions
function onRequest (request, response) {
    var pathname = url.parse (request.url) .pathname; // get the request URL

    // RequestHandlers the handle attribute is an object, the object contains multiple sets of properties: property value, the property name corresponding to the uri, property values corresponding to handler, 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 ( "you visit the page does not exist visit < a href='/'> Home < /a>!");
        response.end ();

var server = http.createServer (onRequest); // function onRequest as createServer parameters, so that the browser sends a request to every server in the function will be called once
server.listen (8888);
// The above two can co-written http.createServer (onRequest) .listen (8888);

The above code can almost become a template for most of the index node project, there will not be great changes, if adding features to the application, you only need to change the file back requestHandlers.js. At school the teacher before the application is divided into a basic node index.js, server.js, router.js and requestHandlers.js four parts, and server.js and router.js always need to change the code, you can applied to any one item, but then I discovered that in fact the first three parts can be integrated together, so on her own for a moment to write the above that index.js, later found after integration actually understand a little better.

Posted below requestHandlers.js code:

var handle = {};
var formidable = require ( "formidable");

handle [ "/"] = start;
handle [ "/ start"] = start;
handle [ "/ upload"] = upload;

function start (request, response) {
  var body = '< html>' +
    '< Head>' +
    '< Meta http-equiv = "Content-Type" content = "text / html;' +
    'Charset = UTF-8 "/>' +
    '< / Head>' +
    '< Body>' +
    '< Form action = "/ upload" method = "post">' +
    'Student ID: < input type = "text" name = "id" /> < br /> < br />' +
    'Name: < input type = "text" name = "name" /> < br /> < br />' +
    '< Input type = "submit" value = "Submit" />' +
    '< / Form>' +
    '< / Body>' +
    '< / Html>';
    response.writeHead (200, { "Content-Type": "text / html"});
    response.write (body);
    response.end ();

function upload (request, response) {
    var form = new formidable.IncomingForm ();
    form.parse (request, function (error, fields, files) {
    console.log ( "id:" + fields.id);
    console.log ( "name:" + fields.name);
    response.writeHead (200, { "Content-Type": "text / html; charset = utf-8"});
    response.write ( "id:" + fields.id + "< br>");
    response.write ( "name:" + fields.name);
    response.end ();

exports.handle = handle;
exports.start = start;
exports.upload = upload;

The above example demonstrates a form after form submission, to extract information from the form of the form. Wherein the handle is an object that contains multiple groups of properties and property values, the corresponding url attribute name, attribute value corresponds to the processing function, the function that is requested url is one to one, each browser sends a request, the server analyzes the request url , with handle in the value of the url attribute named corresponding to the function to process the request. For example, the browser sends a request, uri is / upload, the server looks up the handler, handler [ "/ upload"] = upload, upload function will be called to handle the request. All modules can be called an external file attributes, functions need to use the Export command exports as of the end of requestHandlers.js. So, if you add features, the page request adds a link or form like / xxx, it is often necessary to make changes in requestHandlers.js file in three steps:

First, increase the handler [ "/ xxx"] = xxx;

Second, increase the function xxx (request, response) {...};

Third, increase exports.xxx = xxx;
- Mac OS X 10.9 build Nginx + MySQL + php-fpm environment (Server)
- Linux port mapping system (routing and forwarding) (Server)
- CentOS6.5 install SVN & visual management tools iF.SVNAdmin (Server)
- Hadoop1.2.1 plug compilation (Server)
- Use top to monitor the remote server (Server)
- C ++ input and output summary _ Input (Programming)
- Hadoop2.0 configuration yarn success (Server)
- Use OpenSSL to generate a certificate (Linux)
- shell script error dirname: invalid option - b (Database)
- Delay for the specified IP port analog network to send and receive packets on Linux (Linux)
- Ubuntu achieve initialization iptables (Linux)
- Atheros AR8161 / AR8162 network card driver problem solving in CentOS 6.4 (Linux)
- Linux desktop system using the remote server in clear text ssh password (Server)
- Lsblk command lists using Linux block device information (Linux)
- Let you Fun Ubuntu desktop eleven weapon (Linux)
- Source encountered problems and solutions when installing SaltStack (Server)
- Linux Network Programming - non-blocking program (Programming)
- Will Laravel become the most successful PHP framework? (Programming)
- Quagga use authentication on strengthening BGP session security (Linux)
- YUM install desktop environment in CentOS (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.