当前位置: 代码迷 >> JavaScript >> nodejs使用nodejs创设简单的静态文件服务器
  详细解决方案

nodejs使用nodejs创设简单的静态文件服务器

热度:803   发布时间:2013-12-26 14:54:33.0
nodejs使用nodejs创建简单的静态文件服务器

转自:http://blog.csdn.net/zxsrendong/article/details/16804867

?

在开始之前,应该好好规划一下项目的文件目录了。我的目录结构如下:

assets放置网站的静态文件css,js,img等;common存放项目的配置文件和一些通用文件;server存放服务处理文件,将要创建的静态文件服务就是放在此目录中; tpl放置的是模板文件也就是网页文件。
文件的下载格式主要是由'Content-Type'的值决定的,要想下载的文件能够正常工作就应该正确的设置不同文件的'Content-Type'值。mime.js文件存放了一些常用mime值:

[javascript]?view plaincopy
?
  1. exports.mime?=?{??
  2. ????"html"?:?"text/html",??
  3. ????"css"??:?"text/css",??
  4. ????"js"???:?"text/javascript",??
  5. ????"json"?:?"application/json",??
  6. ????"ico"??:?"image/x-icon",??
  7. ????"gif"??:?"image/gif",??
  8. ????"jpeg"?:?"image/jpeg",??
  9. ????"jpg"??:?"image/jpeg",??
  10. ????"png"??:?"image/png",??
  11. ????"pdf"??:?"application/pdf",??
  12. ????"svg"??:?"image/svg+xml",??
  13. ????"swf"??:?"application/x-shockwave-flash",??
  14. ????"tiff"?:?"image/tiff",??
  15. ????"txt"??:?"text/plain",??
  16. ????"wav"??:?"audio/x-wav",??
  17. ????"wma"??:?"audio/x-ms-wma",??
  18. ????"wmv"??:?"video/x-ms-wmv",??
  19. ????"xml"??:?"text/xml"??
  20. };??

先来看server.js和FServer.js的类容:

?

?

[javascript]?view plaincopy
?
  1. //??server.js????
  2. ????var?config?=?require('./common/config');????
  3. ????var?http???=?require('http');????
  4. ????var?fs?????=?require('fs');????
  5. ????var?url????=?require('url');????
  6. ????var?path???=?require('path');????
  7. ????var?FServer???=?require('./server/FServer');????
  8. ????function?index(){????
  9. ????????var?indexPath?=?config.ui?+?'/index.html';????
  10. ????????fs.exists(indexPath,?function(exists){????
  11. ????????????if(?!exists?)?{????
  12. ????????????????throw?err;????
  13. ????????????}?else?{????
  14. ????????????????fs.readFile(indexPath,?function(err,?data){????
  15. ????????????????????if?(err)?{????
  16. ????????????????????????throw?err;??????
  17. ????????????????????}?else?{????
  18. ????????????????????????function?onRequest(req,?res){????
  19. ????????????????????????????//?取得文件路径????
  20. ????????????????????????????var?pathname?=?url.parse(req.url).pathname;????
  21. ????????????????????????????//?获取文件扩展名(包含前置.)????
  22. ????????????????????????????var?extname?=?path.extname(?pathname?);????
  23. ????????????????????????????var?type?=?extname.slice(1);????
  24. ????????????????????????????//?获取下载文件在磁盘上的路径,????
  25. ????????????????????????????var?realPath?=?config.root?+?pathname;????
  26. ????????????????????????????if?(?extname?===?''?)?{????
  27. ????????????????????????????????res.writeHead(200,?{'Content-Type':'text/html'});????
  28. ????????????????????????????????res.write(data);????
  29. ????????????????????????????????res.end();????
  30. ????????????????????????????}?else?{????
  31. ????????????????????????????????FServer.filesLoad(realPath,?type,?req,?res);????
  32. ????????????????????????????}????
  33. ????????????????????????}????
  34. ????????????????????????http.createServer(onRequest).listen(config.port);????
  35. ????????????????????}????
  36. ????????????????})????
  37. ????????????}????
  38. ????????})????
  39. ?????}????
  40. ????exports.index?=?index;????
  41. ????
  42. ????//?FServer.js????
  43. ????var?fs???=?require('fs');????
  44. ????var?mime?=?require('../common/mime').mime;????
  45. ????function?filesLoad(filePath,?type,?req,?res){????
  46. ????????fs.exists(filePath,?function(exists){????
  47. ????????????if?(?!exists?)?{????
  48. ????????????????res.writeHead(404,?{'Content-Type':?'text/plain'});????
  49. ????????????????//?res.write();????
  50. ????????????????res.end();????
  51. ????????????}?else?{????
  52. ????????????????fs.readFile(filePath,?'binary',?function(err,?file){????
  53. ????????????????????if?(?err?)?{????
  54. ????????????????????????res.writeHead(500,?{'Content-Type':?'text/plain'});????
  55. ????????????????????????//?res.write();????
  56. ????????????????????????res.end();????
  57. ????????????????????}?else?{????
  58. ????????????????????????res.writeHead(200,?{'Content-Type':?mime[type]});????
  59. ????????????????????????res.write(file,?'binary');????
  60. ????????????????????????res.end();????
  61. ????????????????????}????
  62. ????????????????});????
  63. ????????????}????
  64. ????????})????
  65. ????}????
  66. ????exports.filesLoad?=?filesLoad;????

上面引入了nodejs的内置模块http、fs、url、path,config和FServer是自定义模块,要读取文件首先要知道文件在磁盘上是否存在,还应当知道文件的类型才能达到想要的效果。运行程序可以发现css和javascript都下载正确,并且css效果在页面上正确渲染(javascript还没有写效果)

?

?