Front end technology frontier 7

//Load an http module var http = require('http'); //Create server to create a WEB server named server var server= http.createServer(functio...
Please praise! Because your encouragement is the biggest driving force of my writing!
//Load an http module var http = require('http'); //Create server to create a WEB server named server var server= http.createServer(function(req,res){//The two parameters of the function are req and res, which are used for request and response respectively res.writeHead(200,{'content-Type':'text/pain'});//The status code written on the returned request header is 200, and the type of returned text content is plain text res.end('Hellow Nodejs\n'); }); server.listen(1337, '127.0.0.1');//listen listens for requests on port 1337, and the server can receive any requests from the port console.log('Server running at http://127.0.0.1:1337/');
var http = require("http"); http.crateServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); }).listen(8888); var http = require("http"); function onRequest(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); } http.createServer(onRequest).listen(8888); //Load an http module var http = require('http'); //Create server to create a WEB server named server var server= http.createServer(function(req,res){//The two parameters of the function are req and res, which are used for request and response respectively res.writeHead(200,{'content-Type':'text/pain'});//The status code written on the returned request header is 200, and the type of returned text content is plain text res.end('Hellow Nodejs\n'); }); server.listen(1337, '127.0.0.1');//listen listens for requests on port 1337, and the server can receive any requests from the port console.log('Server running at http://127.0.0.1:1337/');

How does the server handle requests

Use the response.writeHead() function to send an HTTP status 200 and content type of HTTP header, and use the response.write() function to send text in the corresponding HTTP body.

Call response.end() to complete the response

var http = require("http"); http.createServer(...);

The script that requests the server module just needs to start the server.

var http = require("http"); function start() { function onRequest(request, response) { console.log("Request received"); response.writeHead(200, {"Context-Type": "text/plain"}); response.write("Hello World"); response.end(); } http.createServer(onRequest).listen(8888); console.log("Server has started"); } exports.start = start;
var server = require("./server"); server.start();

Routing selection
First parameter passing of onRequest() callback function
url and querystring modules

image.png
var http = require("http"); var url = require("url"); function start() { function onRequest(request, response) { var pathname = url.parse(request.url).pathname; console.log("Request for " + pathname + " received."); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); } http.createServer(onRequest).listen(8888); console.log("Server has started."); } exports.start = start;
function route(pathname) { console.log("About to route a request for " + pathname); } exports.route = route;
image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png

requestHandler.js modify


image.png
function start(response) { console.log("Request handler 'start' was called."); var body = '<html>'+ '<head>'+ '<meta http-equiv="Content-Type" content="text/html; '+ 'charset=UTF-8" />'+ '</head>'+ '<body>'+ '<form action="/upload" method="post">'+ '<textarea name="text" rows="20" cols="60"></textarea>'+ '<input type="submit" value="Submit text" />'+ '</form>'+ '</body>'+ '</html>'; response.writeHead(200, {"Content-Type": "text/html"}); response.write(body); response.end(); } function upload(response) { console.log("Request handler 'upload' was called."); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello Upload"); response.end(); } exports.start = start; exports.upload = upload;
As follows: request.addListener("data", function(chunk) { // called when a new chunk of data was received }); request.addListener("end", function() { // called when all chunks of data have been received });
First from server.js Start: 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(8888); console.log("Server has started."); } exports.start = start;
function route(handle, pathname, response, postData) { console.log("About to route a request for " + pathname); if (typeof handle[pathname] === 'function') { handle[pathname](response, postData); } else { console.log("No request handler found for " + pathname); response.writeHead(404, {"Content-Type": "text/plain"}); response.write("404 Not found"); response.end(); } } exports.route = route;

In requestHandlers.js, we include the data in the response to the upload request:

function start(response, postData) { console.log("Request handler 'start' was called."); var body = '<html>'+ '<head>'+ '<meta http-equiv="Content-Type" content="text/html; '+ 'charset=UTF-8" />'+ '</head>'+ '<body>'+ '<form action="/upload" method="post">'+ '<textarea name="text" rows="20" cols="60"></textarea>'+ '<input type="submit" value="Submit text" />'+ '</form>'+ '</body>'+ '</html>'; response.writeHead(200, {"Content-Type": "text/html"}); response.write(body); response.end(); } function upload(response, postData) { console.log("Request handler 'upload' was called."); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("You've sent: " + postData); response.end(); } exports.start = start; exports.upload = upload;

The querystring module implements:

var querystring = require("querystring"); function start(response, postData) { console.log("Request handler 'start' was called."); //Building application modules 33 var body = '<html>'+ '<head>'+ '<meta http-equiv="Content-Type" content="text/html; '+ 'charset=UTF-8" />'+ '</head>'+ '<body>'+ '<form action="/upload" method="post">'+ '<textarea name="text" rows="20" cols="60"></textarea>'+ '<input type="submit" value="Submit text" />'+ '</form>'+ '</body>'+ '</html>'; response.writeHead(200, {"Content-Type": "text/html"}); response.write(body); response.end(); } function upload(response, postData) { console.log("Request handler 'upload' was called."); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("You've sent the text: "+ querystring.parse(postData).text); response.end(); } exports.start = start; exports.upload = upload;

Process file upload

image.png
node-formidable The official example shows how the two parts work together: var formidable = require('formidable'), http = require('http'), sys = require('sys'); http.createServer(function(req, res) { if (req.url == '/upload' && req.method.toLowerCase() == 'post') { // parse a file upload var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files) { res.writeHead(200, {'content-type': 'text/plain'}); res.write('received upload:\n\n'); res.end(sys.inspect()); }); return; } // show a file upload form res.writeHead(200, {'content-type': 'text/html'}); res.end( '<form action="/upload" enctype="multipart/form-data" '+ 'method="post">'+ '<input type="text" name="title"><br>'+ '<input type="file" name="upload" multiple="multiple"><br>'+ '<input type="submit" value="Upload">'+ '</form>' ); }).listen(8888);
image.png

Please praise! Because your encouragement is the biggest driving force of my writing!

Official WeChat public address

Forced communication group: 711613774

Push communication group

6 November 2019, 16:45 | Views: 7765

Add new comment

For adding a comment, please log in
or create account

0 comments