打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Node.js开发手册(二)
作者:chszs,转载需注明。博客主页:http://blog.csdn.net/chszs

一、静态文件服务

如果我们想把磁盘上的文件作为Web内容对外提供服务,那么怎么用Node来实现呢?答案是使用fs模块。
我们可以使用fs即filesystem模块载入文件的内容,并把内容通过createServer回调函数传递出去。提供静态文件服务是Node.js的核心基础功能,应该掌握它。

二、Demo

下面我们创建三个文件,这些文件都会由NodeJS提供静态文件服务。

1. index.html

内容如下:
  1. <html>  
  2.     <head>  
  3.         <title>Yay Node!</title>  
  4.         <link rel="stylesheet" href="styles.css" type="text/css" />  
  5.         <script src="script.js" type="text/javascript"></script>  
  6.     </head>  
  7.     <body>  
  8.         <span id="yay">Yay!</span>  
  9.     </body>  
  10. </html>  

2. script.js

内容如下:
[javascript] view plain copy
print?
  1. // script.js  
  2. window.onload=function(){  
  3.     alert('Yay Node!');  
  4. };  

3. styles.css

内容如下:
  1. #yay{  
  2.     font-size: 5em;  
  3.     background: blue;  
  4.     color: yellow;  
  5.     padding: 0.5em;  
  6. }  

将上面的三个文件放入content目录下。

4. 编写Node程序

创建名为server.js的脚本文件,内容如下:
[javascript] view plain copy
print?
  1. var http = require('http');  
  2. var path = require('path');  
  3. var fs = require('fs');  
  4.   
  5. http.createServer(function(req, res){  
  6.     var lookup = path.basename(decodeURI(req.url)) || 'index.html',  
  7.     f = 'content/' + lookup;  
  8.     fs.exists(f, function(exists){  
  9.         console.log(exists ? lookup + " is there" : lookup + " doesn't exist");  
  10.     });  
  11. }).listen(8080);  

用supervisor热部署工具执行以上程序:
$ supervisor server.js

在浏览器访问地址:http://localhost:8080/foo
终端显示:
DEBUG: Starting child process with 'node server.js'
foo doesn't exist

在浏览器访问地址:http://localhost:8080/index.html
终端显示:
DEBUG: Starting child process with 'node server.js'
DEBUG: Watching directory '/home/chszs/tmp' for changes.
index.html is there

三、进阶

假定我们想让客户端知道文件的类型,可以通过扩展名进行判断。
修改server.js文件,内容为:
[javascript] view plain copy
print?
  1. // server.js  
  2. var http = require('http');  
  3. var path = require('path');  
  4. var fs = require('fs');  
  5.   
  6. var mimeTypes = {  
  7.     '.js': 'text/javascript',  
  8.     '.html': 'text/html',  
  9.     '.css': 'text/css'  
  10. };  
  11.   
  12. http.createServer(function(req, res){  
  13.     var lookup = path.basename(decodeURI(req.url)) || 'index.html',  
  14.     f = 'content/' + lookup;  
  15.     fs.exists(f, function(exists){  
  16.         if(exists){  
  17.             fs.readFile(f, function(err, data){  
  18.                 if(err){  
  19.                     res.writeHead(500);  
  20.                     res.end('Server Error!');  
  21.                     return;  
  22.                 }  
  23.                 var headers = {'Content-type': mimeTypes[path.extname]};  
  24.                 res.writeHead(200, headers);  
  25.                 res.end(data);  
  26.             });  
  27.             return;  
  28.         }  
  29.         res.writeHead(404);  
  30.         res.end();  
  31.     });  
  32. }).listen(8080);  

在浏览器地址栏输入
http://localhost:8080/styles.css

http://localhost:8080/script.js
都会在网页上显示文件的内容。

在浏览器地址栏输入
http://localhost:8080/index.html
如图所示:



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Node.js基础入门第八天
nodejs入门
critters 开发包的使用案例分享
JS的dtree和xtree介绍
javascript – Chrome说“资源解释为样式表,但使用MIME类型text / html传输”
全栈工程师终极修炼指南
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服