• node本地服务,访问本地静态文件,用于手机本地查看


    项目目录:

      webapp  //访问的文件

      http.js  

      mine.js

    http.js:

    var PORT = 3000;//
    
    var http = require('http');
    var url=require('url');
    var fs=require('fs');
    var mine=require('./mine').types;//
    var path=require('path');
    var pulichPuch='';  //本地资源地址
    
    var server = http.createServer(function (request, response) {
        var pathname = url.parse(request.url).pathname;
        var realPath = path.join(pulichPuch?pulichPuch:"webapp", pathname);    //这里设置自己的文件名称;
        console.log(realPath)
    
        var ext = path.extname(realPath);
        ext = ext ? ext.slice(1) : 'unknown';
        fs.exists(realPath, function (exists) {
            if (!exists) {
                response.writeHead(404, {
                    'Content-Type': 'text/plain'
                });
    
                response.write("This request URL " + pathname + " 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";
                        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",
        "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"
    };
    

      

  • 相关阅读:
    我爱网络流之最大流Dinic
    2019ICPC陕西邀请赛复盘
    ZOJ
    ACM省赛及蓝桥总结,且随疾风前行,身后亦须留心
    ZOJ 4124 拓扑排序+思维dfs
    蓝桥 log大侠
    蓝桥历年试题 DNA对比
    “美登杯”上海市高校大学生程序设计邀请赛 (华东理工大学)
    蓝桥历年试题 套娃
    WPF学习之路(七)应用程序和窗口(续)
  • 原文地址:https://www.cnblogs.com/eunuch/p/16189966.html
Copyright © 2020-2023  润新知