步骤:
1、从官网https://nodejs.org/下载node.exe执行文件
2、使用文本编辑器编写服务器js脚本;
test.js
点击(此处)折叠或打开
- var PORT = 8888;
- var http = require('http');
- var url=require('url');
- var fs=require('fs');
- var mine=require('./mine').types;
- var path=require('path');
- var Tools=require('./Tools');
- //var tools = new Tools();
- var server = http.createServer(function (request, response) {
- var pathname = url.parse(request.url).pathname;
- var guessPage = Tools.guessPage(fs, path, path.dirname(process.execPath), pathname);
- var realPath = guessPage.realPath;
- var ext = guessPage.ext;
-
- fs.exists(realPath, function (exists) {
- if (!exists) {
- response.writeHead(404, {
- 'Content-Type': 'text/plain'
- });
- response.write("This request URL " + realPath + " was not found on this server.");
- response.end();
- } else {
- fs.readFile(realPath, "binary", function (err, file) {
- if (err) {
- response.writeHead(500, {
- 'Content-Type': 'text/plain'
- });
- response.end(err);
- } else {
- var contentType = mine[ext] || "text/plain";
- console.log("contentType: " + contentType);
- response.writeHead(200, {
- 'Content-Type': contentType
- });
- response.write(file, "binary");
- response.end();
- }
- });
- }
- });
- });
- server.listen(PORT);
- console.log("Server runing at port: " + PORT + ".");
点击(此处)折叠或打开
- exports.types = {
- "css": "text/css",
- "gif": "image/gif",
- "htm": "text/html",
- "html": "text/html",
- "ico": "image/x-icon",
- "jpeg": "image/jpeg",
- "jpg": "image/jpeg",
- "js": "text/javascript",
- "json": "application/json",
- "pdf": "application/pdf",
- "png": "image/png",
- "svg": "image/svg+xml",
- "swf": "application/x-shockwave-flash",
- "tiff": "image/tiff",
- "txt": "text/plain",
- "wav": "audio/x-wav",
- "wma": "audio/x-ms-wma",
- "wmv": "video/x-ms-wmv",
- "xml": "text/xml"
- };
点击(此处)折叠或打开
- // if export Object, use constructor define
- //function Tools() {};
- // export Object
- //module.exports = Tools;
- /**
- * 补充请求页面,自动添加index.html/index.htm
- */
- //Tools.guessPage
- exports.guessPage = function(fs, path, curDir, pathname) {
- console.log("pathname: " + pathname);
- if (!pathname) {
- pathname = pathname+"/";
- }
- var realPath = path.join(curDir, pathname);
- console.log("realPath: " + realPath);
- var ext = path.extname(realPath);
- console.log("before ext: " + ext);
- if (!ext) {
- // guess index.html, is not exist, then index.htm.
- var tmpPath = realPath + "index.htm";
- console.log("tmpPath: " + tmpPath);
- // fs.existsSync will be deprecated.
- // var exists = fs.existsSync(tmpPath);
- var exists = true;
- try {
- fs.openSync(tmpPath, "r");
- } catch (e) {
- console.log("e: " + e);
- exists = false;
- }
- console.log("exists: " + exists);
- if (exists) {
- ext = "htm";
- } else {
- tmpPath = realPath + "index.html";
- ext = "html";
- }
- realPath = tmpPath;
- } else {
- ext = ext.slice(1);
- }
- console.log("ext: "+ext);
- var result = new Object();
- result.realPath = realPath;
- result.ext = ext;
- return result;
- }
4、访问web服务器:http://127.0.0.1:8888,默认会导航到web应用根目录下的index.htm/index.html。
注:本文将服务器js脚本拆分成了3个文件,是为了了解和学习nodejs的模块化编码,实际环境下可以使用一个js文件完成同样的任务。
参考资料:
http://www.cnblogs.com/shawn-xie/archive/2013/06/06/3121173.html
https://nodejs.org/api/