node. JS-----Processing Post Requests

Source: Internet
Author: User

Simple Demo,post data, and accept the data

Modules for Requesthandlers

Start (): Post form HTML

Upload:post Data Acquisition Page

-----requesthandlers.js

-----router.js

-----server.js

-----index.js

Requesthandlers.js

/* Simple but demo post data, and accept Data *//*requesthandlers.js module */function start (response,postdata) {console.log ("Request handler" Start ' was called '); var body = ' 

  

Router.js

The/*routers.js module *//* by checking that the request handler for the given path exists, calling the corresponding function directly if it exists, and returning the corresponding string */function route (Handle,pathname,response, PostData) {Console.log ("about-route a request for" + pathname); if (typeof handle[pathname] = = = ' function ') {HANDLE[PATHN AME] (response,postdata);} Else{console.log ("No Request handler for  

  

Server.js

/*server Module */var http = require ("http"), var url = require ("url"), function start (route, handle) {function onrequest (  Request, Response) {var postdata = ""; var pathname = Url.parse (Request.url). Pathname;console.log ("Request for" + pathname + "received"); Request.setencoding ("UTF8"); Request.addlistener ("Data", function (postdatachunk) {postdata + = Postdatachunk;console.log ("Received POST data Chunk" + Postdatachunk + "."); Request.addlistener ("End", function () {route (handle, pathname, response, postdata);}); Http.createserver (onrequest). Listen, Console.log ("Server has Started");} Exports.start = start;

 

Index.js

/*index module */var server = require ("./server"); var router = require ("./router"); var requesthandlers = require ("./requesthandlers");  var handle = {}//Case-sensitive  handle["/"] = Requesthandlers.start; handle["/start"] = Requesthandlers.start; handle["/ Upload "] = requesthandlers.upload;  

Command line launch app:

Node Index.js

  

Browser input:

Http://127.0.0.1:3000/

  

The command line displays:

Browser display:

After submit, the command line displays: The URL changes at this time, calling the/upload handler.

The command line displays:

Browser display: The Response object returns the data displayed to the user:

node. JS-----Processing Post Requests

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.