关于别的电脑访问node.js建的服务器

来源:互联网 发布:淘宝网商城广场舞服装 编辑:程序博客网 时间:2024/05/01 16:18

1、你得首先安装node.js,官网地址https://nodejs.org/下载node.exe执行文件,傻瓜式按装

2、按装完后,在node的安装位置


3、红色地方建三个文件,test.js,mine.js,Tools.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 + ".");



打开 mine.js并把下面这段代码复制进去

    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"};

打开 Tools.js并把下面这段代码复制进去

    // if export Object, use constructor define//function Tools() {};// export Object//module.exports = Tools;/** * 补充请求页面,自动添加index.html/index.htm *///Tools.guessPageexports.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、没写完,有空继续写

原创粉丝点击