简单的Web服务器: 作者本身也是刚接触nodejs,所以在知识面方面还存在很多漏洞
由于一直到在使用nodejs搭建博客Hexo,但是还想使用服务器,搭建服务器的方法有很多种,但是既然有了NodeJs这样的大牛,我也就懒得用其他的了。
1 2 3 4 5 6 7 8 直接上代码: 首先我们创建一个WebSvr.js,将其放置在文件夹中,通过代码进行启动。 spfk@spfk-PC MINGW64 $ cd e:/Github spfk@spfk-PC MINGW64 /e/Github $ node.exe WebSvr.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 var http = require ('http' ); var fs = require ('fs' ); var server = new http.Server(); server.listen(4000 ); server.on('request' , function (request, response ) { console .log('Request' ); var url = require ('url' ).parse(request.url); switch (url.pathname) { case '' ||'/' : fs.readFile('./index.html' , function (err, content ) { if (err) { response.writeHead(404 , { 'Content-Type' :'text/plain; charset="UTF-8"' }); response.write(err.message); response.end(); } else { response.writeHead(200 , { 'Content-Type' : 'text/html; charset=UTF-8' }); response.write(content); response.end(); } }); break ; case '/test/delay' : var delay = parseInt (url.query) || 2000 ; response.writeHead(200 , {'Content-type' :'text/plain; charset=UTF-8' }); response.write('Sleeping for' + delay + ' milliseconds...' ); setTimeout(function ( ) { response.write('done.' ); response.end(); }, delay); break ; case '/test/mirror' : response.writeHead(200 , {'Content-type' :'text/plain; charset=UTF-8' }); response.write(request.mothod + ' ' + request.url + ' HTTP/' + request.httpVersion + '\r\n' ); for (var h in request.headers) { response.write(h + ':' + request.headers[h] + '\r\n' ); } response.write('\r\n' ); request.on('data' , function (chunk ) { response.write(chunk); }); request.on('end' , function (chunk ) { response.end(); }); break ; case '/json' : response.writeHead(200 , {'Content-type' :'application/json; charset=UTF-8' }); response.write(JSON .stringify({test:'success' })); response.end(); break ; default : var filename = url.pathname.substring(1 ); var type = getType(filename.substring(filename.lastIndexOf('.' )+1 )); fs.readFile(filename, function (err, content ) { if (err) { response.writeHead(404 , { 'Content-Type' :'text/plain; charset="UTF-8"' }); response.write(err.message); response.end(); } else { response.writeHead(200 , { 'Content-Type' : type }); response.write(content); response.end(); } }); break ; } }); function getType (endTag ) { var type =null ; switch (endTag){ case 'html' : case 'htm' : type = 'text/html; charset=UTF-8' ; break ; case 'js' : type = 'application/javascript; charset="UTF-8"' ; break ; case 'css' : type = 'text/css; charset="UTF-8"' ; break ; case 'txt' : type = 'text/plain; charset="UTF-8"' ; break ; case 'manifest' : type = 'text/cache-manifest; charset="UTF-8"' ; break ; default : type = 'application/octet-stream' ; break ; } return type ; } console .log('[WebServer][Start] running at http://localhost:4000' );
<
HTML结构零散
JS实现继承的几种方式
>