Web 服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,可以向浏览器等 Web 客户端提供文档,也可以放置网站文件,让全世界浏览;可以放置数据文件,让
全世界下载。目前最主流的三个 Web 服务器是 Apache Nginx IIS
目录如下
通过启动node服务器,打开本地去访问static下面的资源
这是http.js代码
// npm install -g supervisor supervisor http.js就可以实现热更新的效果 //引入http模块 var http=require('http'); //fs模块 var fs=require('fs'); //path模块 var path=require('path'); /*nodejs自带的模块*/ //url模块 var url=require('url'); var mimeModel=require('./model/getmime.js'); //console.log(mime.getMime('.css')); //获取文件类型 http.createServer(function(req,res){ //http://localhost:8001/news.html /news.html //http://localhost:8001/index.html /index.html //css/dmb.bottom.css var pathname=url.parse(req.url).pathname; console.log(pathname); if(pathname=='/'){ pathname='/index.html'; /*默认加载的首页*/ } //获取文件的后缀名 var extname=path.extname(pathname); if(pathname!='/favicon.ico'){ /*过滤请求favicon.ico*/ //console.log(pathname); //文件操作获取 static下面的index.html fs.readFile('static/'+pathname,function(err,data){ if(err){ /*么有这个文件*/ console.log('404'); fs.readFile('static/404.html',function(error,data404){ if(error){ console.log(error); } res.writeHead(404,{"Content-Type":"text/html;charset='utf-8'"}); res.write(data404); res.end(); /*结束响应*/ }) }else{ /*返回这个文件*/ var mime=mimeModel.getMime(extname); /*获取文件类型*/ res.writeHead(200,{"Content-Type":""+mime+";charset='utf-8'"}); res.write(data); res.end(); /*结束响应*/ } }) } }).listen(8002); console.log('服务器已开启---------------------------------------------------------------端口号自己看-----------------------------------------------');
这是getmime.js代码
exports.getMime=function(extname){ switch (extname){ case '.html': return 'text/html'; case '.css': return 'text/css'; case '.js': return 'text/javascript'; default: return 'text/html'; } }
// npm install -g supervisor supervisor http.js就可以实现热更新的效果
启动node
supervisor http.js打开http://localhost:8002/就可以看到效果了