Chinaunix首页 | 论坛 | 博客
  • 博客访问: 21072
  • 博文数量: 5
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 50
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-14 20:54
个人简介

写点东西,帮忙记点东西。

文章分类

全部博文(5)

文章存档

2015年(5)

我的朋友

分类: JavaScript

2015-05-27 21:43:45

环境:Windows XP

步骤
1、从官网下载node.exe执行文件
2、使用文本编辑器编写服务器js脚本;
test.js

点击(此处)折叠或打开

  1. var PORT = 8888;

  2. var http = require('http');
  3. var url=require('url');
  4. var fs=require('fs');
  5. var mine=require('./mine').types;
  6. var path=require('path');
  7. var Tools=require('./Tools');
  8. //var tools = new Tools();

  9. var server = http.createServer(function (request, response) {
  10.     var pathname = url.parse(request.url).pathname;
  11.     var guessPage = Tools.guessPage(fs, path, path.dirname(process.execPath), pathname);
  12.     var realPath = guessPage.realPath;
  13.     var ext = guessPage.ext;
  14.         
  15.     fs.exists(realPath, function (exists) {
  16.         if (!exists) {
  17.             response.writeHead(404, {
  18.                 'Content-Type': 'text/plain'
  19.             });

  20.             response.write("This request URL " + realPath + " was not found on this server.");
  21.             response.end();
  22.         } else {
  23.             fs.readFile(realPath, "binary", function (err, file) {
  24.                 if (err) {
  25.                     response.writeHead(500, {
  26.                         'Content-Type': 'text/plain'
  27.                     });
  28.                     response.end(err);
  29.                 } else {
  30.                     var contentType = mine[ext] || "text/plain";
  31.                     console.log("contentType: " + contentType);
  32.                     response.writeHead(200, {
  33.                         'Content-Type': contentType
  34.                     });
  35.                     response.write(file, "binary");
  36.                     response.end();
  37.                 }
  38.             });
  39.         }
  40.     });
  41. });
  42. server.listen(PORT);
  43. console.log("Server runing at port: " + PORT + ".");
mine.js

点击(此处)折叠或打开

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

点击(此处)折叠或打开

  1. // if export Object, use constructor define
  2. //function Tools() {};
  3. // export Object
  4. //module.exports = Tools;

  5. /**
  6.  * 补充请求页面,自动添加index.html/index.htm
  7.  */
  8. //Tools.guessPage
  9. exports.guessPage = function(fs, path, curDir, pathname) {
  10.     console.log("pathname: " + pathname);
  11.     if (!pathname) {
  12.         pathname = pathname+"/";
  13.     }
  14.     var realPath = path.join(curDir, pathname);
  15.     console.log("realPath: " + realPath);
  16.     var ext = path.extname(realPath);
  17.     console.log("before ext: " + ext);
  18.     if (!ext) {
  19.         // guess index.html, is not exist, then index.htm.
  20.         var tmpPath = realPath + "index.htm";
  21.         console.log("tmpPath: " + tmpPath);
  22.         // fs.existsSync will be deprecated.
  23.         // var exists = fs.existsSync(tmpPath);
  24.         var exists = true;
  25.         try {
  26.             fs.openSync(tmpPath, "r");
  27.         } catch (e) {
  28.             console.log("e: " + e);
  29.             exists = false;
  30.         }
  31.         console.log("exists: " + exists);
  32.         if (exists) {
  33.             ext = "htm";
  34.         } else {
  35.             tmpPath = realPath + "index.html";
  36.             ext = "html";
  37.         }
  38.         realPath = tmpPath;
  39.     } else {
  40.         ext = ext.slice(1);
  41.     }
  42.     console.log("ext: "+ext);
  43.     var result = new Object();
  44.     result.realPath = realPath;
  45.     result.ext = ext;
  46.     return result;
  47. }
3、将这些js脚本和node.exe一起拷贝到web应用根目录下,执行dos命令或者直接拖动test.js文件放在node.exe文件上,服务器环境便启动了,支持html/css/js的静态web服务器应用。

4、访问web服务器:,默认会导航到web应用根目录下的index.htm/index.html。


注:本文将服务器js脚本拆分成了3个文件,是为了了解和学习nodejs的模块化编码,实际环境下可以使用一个js文件完成同样的任务。

参考资料:
http://www.cnblogs.com/shawn-xie/archive/2013/06/06/3121173.html


阅读(10211) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~