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

推荐订阅源

P
Proofpoint News Feed
H
Hacker News: Front Page
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cisco Blogs
P
Palo Alto Networks Blog
Know Your Adversary
Know Your Adversary
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
S
Schneier on Security
The Hacker News
The Hacker News
Cyberwarzone
Cyberwarzone
T
Tenable Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Tailwind CSS Blog
S
Secure Thoughts
N
Netflix TechBlog - Medium
T
The Exploit Database - CXSecurity.com
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Help Net Security
Help Net Security
K
Kaspersky official blog
Google Online Security Blog
Google Online Security Blog
L
LangChain Blog
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
Hacker News: Ask HN
Hacker News: Ask HN
www.infosecurity-magazine.com
www.infosecurity-magazine.com
有赞技术团队
有赞技术团队
P
Privacy International News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recent Announcements
Recent Announcements
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Register - Security
The Register - Security
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
Recorded Future
Recorded Future
The Last Watchdog
The Last Watchdog
G
Google Developers Blog
T
Threatpost
小众软件
小众软件
S
Securelist
Recent Commits to openclaw:main
Recent Commits to openclaw:main
O
OpenAI News

轶哥博客

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 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 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 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