//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