nodejs搭建本地服务器
发布日期:2019-01-05 13:59 | 文章来源:网络
香港服务器
香港CN2服务器、企业级CN2 GIA双程专线高速回国T3机房香港、美国、韩国、海外物理服务器热销中!
varhttp=require;varfs=require;varurl=require;//创建服务器http.createServer{//解析请求,包括文件名varpathname=url.parse.pathname;//默认加载路径if{pathname="/index.html";}//输出请求的文件名console.log;//获取后缀,判断是js还是css文件,如果目录结构不同,此处需要修改varfirstDir=pathname&&pathname.split1;varContentType={'Content-Type':'text/html'};//js-application/x-javascriptif>-1){ContentType={'Content-Type':'text/css'};}if>-1){ContentType={'Content-Type':'image/png'};}if>-1){ContentType={'Content-Type':'application/x-javascript'}}//从文件系统中去请求的文件内容fs.readFile,function{if{console.log;//HTTP状态码404:NOTFOUND//ContentType:text/plainresponse.writeHead;//发送响应数据response.end;}elseif>-1){//HTTP状态码:200:OK//ContentType:text/plainresponse.writeHead;varimageFilePath=pathname.substr;varstream=fs.createReadStream;varresponseData=;//存储文件流if{//判断状态stream.on{responseData.push;});stream.on{varfinalData=Buffer.concat;response.write;response.end;});}}else{//HTTP状态码200:OK//ContentType:text/plainresponse.writeHead;//写会回相应内容response.write);//发送响应数据response.end;}});}).listen;console.log;
Tags:香港服务器,nodejs搭建本地服务器
版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。
相关文章
下一篇: