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