惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
爱范儿
爱范儿
罗磊的独立博客
V
V2EX
量子位
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
Jina AI
Jina AI
博客园 - 叶小钗
小众软件
小众软件
博客园 - 【当耐特】
Y
Y Combinator Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
博客园 - 聂微东
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell

轶哥博客

blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog
blog
2016-02-05 · via 轶哥博客

  使用Node.js模拟一个Nginx/Apache静态文件服务器,可以直接运行HTML及JavaScript、各类图片。代码来源于网络,具体出处不详,完善类型后贡献于网络。完美支持SAE的容器。

  server.js文件如下(请与其它html等静态文件位于同一根目录):

// 用HTTP://localhost:5050访问这个服务器

// 首先加载所有需要用到的模块
var http = require('http');        // Http服务器API
var fs = require('fs');            // 用于处理本地文件
var server = new http.Server();    // 创建新的HTTP服务器
var PORT = 5050;                   // 监听端口5050
server.listen(PORT);
console.log('Server runing at port: ' + PORT);
// 使用on方法注册时间处理
server.on('request', function(request, response) { // 当有request请求的时候触发处理函数
    // 解析请求的URL
    var url = require('url').parse(request.url);
    console.log('request' + url.pathname);
    // 特殊URL会让服务器在发送响应前先等待,模拟路由
    switch(url.pathname) {
        case ''||'/' : // 模拟欢迎页,nodejs是高效流处理的方案,也可以通过配置文件来配置
            fs.readFile('index.html', function(err, content){
                if(err) {
                    response.writeHead(404, { 'Content-Type':'text/plain; charset="UTF-8"' });
                    response.write(err.message);
                    response.end();
                } else {
                    response.writeHead(200, { 'Content-Type' : 'text/html; charset=UTF-8' });
                    response.write(content);
                    response.end();
                }
            });
            break;
        //case '/json' : // 模拟JSON数据返回
        //    // 响应状态和头
        //    response.writeHead(200, {'Content-type':'application/json; charset=UTF-8'});
        //    response.write(JSON.stringify({test:'success'}));
        //    response.end();
        //    break;
        default:// 处理来自本地目录的文件
            var filename = url.pathname.substring(1);    // 去掉前导'/'
            var type = getType(filename.substring(filename.lastIndexOf('.')+1));
            // 异步读取文件,并将内容作为单独的数据模块传给回调函数
            // 对于确实很大的文件,使用流API fs.createReadStream()更好
            fs.readFile(filename, function(err, content){
                if(err) {
                    response.writeHead(404, { 'Content-Type':'text/plain; charset="UTF-8"' });
                    response.write(err.message);
                    response.end();
                } else {
                    response.writeHead(200, { 'Content-Type' : type });
                    response.write(content);
                    response.end();
                }
            });
            break;
    }

});
function getType(endTag){
    var type=null;
    switch(endTag){
        case 'html' :
        case 'htm' :
            type = 'text/html; charset=UTF-8';
            break;
        case 'js' :
            type = 'application/javascript; charset="UTF-8"';
            break;
        case 'css' :
            type = 'text/css; charset="UTF-8"';
            break;
        case 'txt' :
            type = 'text/plain; charset="UTF-8"';
            break;
        case 'gif' :
            type = 'image/gif; charset="UTF-8"';
            break;
        case 'manifest' :
            type = 'text/cache-manifest; charset="UTF-8"';
            break;
        case 'ico' :
            type = 'image/x-icon; charset="UTF-8"';
            break;
        case 'jpg' :
        case 'jpeg' :
            type = 'image/jpeg; charset="UTF-8"';
            break;
        case 'pdf' :
            type = 'application/pdf; charset="UTF-8"';
            break;
        case 'png' :
            type = 'image/png; charset="UTF-8"';
            break;
        case 'svg' :
            type = 'image/svg+xml; charset="UTF-8"';
            break;
        default :
            type = 'application/octet-stream';
            break;
    }
    return type;
}

如果用于SAE,还需要一个package.json文件,内容可以参考:

{
  "name": "runyikeji",
  "version": "0.0.3",
  "private": true,
  "scripts": {
    "start": "node ./server.js"
  },
  "dependencies": {
    "body-parser": "~1.13.2",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "express": "~4.13.1",
    "jade": "~1.11.0",
    "morgan": "~1.6.1",
    "serve-favicon": "~2.3.0"
  }
}

顺便补充一下SAE进行GIT操作的常用命令:

本地操作SAE的git操作命令

提交:git push saegggj master:1

删除:git push saegggj --delete 1 或 git push sae :1

查看远程分支:git branch -a

2017年05月03日16:34:37更新:

  可以参考润轶公司开源的库,里面有更全面的Node.js版本静态资源服务器代码:
  https://github.com/runyikeji/nodeStaticServer