Node.js--dns Parsing Demo

Source: Internet
Author: User

//Introducing ModulesvarHTTP = require (' http '), URL= require (' URL '), QueryString= Require (' querystring '), FS= Require (' FS '), DNS= Require (' DNS ');//display fill out the page. Form form for output query by Res.endvarGoindex =function(req, res) {Res.end (");};//DNS parsing functions. Through the Dns.resolve parsing of the DNS module, the parse executes the callback successfully and writes the value to the parameter function in the callback .varGodns =function(PostData, callback) {varDomainvalue = Querystring.parse (postdata). Search_dns;//get the Search_dns in post dataDns.resolve (Domainvalue,function(err, addresses) {if(!addresses) {Addresses= [' Domain name does not exist '];    } callback (Domainvalue, addresses); });};//DNS resolution. Get post data, parse with DNS module, output parse resultvarParsedns =function(req, res) {//read the data that came from the post    varPostData = ' '; //The Req Request object has a AddListener method, and the ' data ' event is triggered when it is transmitted to the server, and ' end ' is triggered when the data is sent.Req.addlistener (' Data ',function(datachunk) {postdata+=Datachunk;    }); Req.addlistener (' End ',function(){//Complete data transfer, DNS resolutionGodns (PostData,function(Domain, addresses) {//Output Parse Results display page in callbackRes.end (");    }); });};varRouter =function(req, res, pathname) {Switch(pathname) { Case'/parse ': Parsedns (req, res);  Break; default: Goindex (req, res); }};http.createserver (function(req, res) {//GET request relative path    varpathname =Url.parse (req.url). Pathname; //set the request to Utf-8 to prevent garbledReq.setencoding (' UTF8 '); //write the returned headerRes.writehead, {' Content-type ': ' text/html '}); //Router Request path if it is/parse DNS resolution, if not show fill pageRouter (req, res, pathname);}) . Listen (3000, ' 127.0.0.1 ');

Reference: "node. JS Development Practical Details" Tsinghua University Press, this article can be regarded as the book's reading notes

Node.js--dns Parsing Demo

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.