使用ruby搭建简易的http服务和sass环境

使用ruby搭建简易的http服务和sass环境

由于在通常的前端开发情况下,我们会有可能需要一个http服务,当然你可以选择自己写一个node的http服务,也比较简单,比如下面的node代码:

var PORT = 3000;

var http = require('http');
var url=require('url');
var fs=require('fs');
var mine= {
  "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"
};
var path=require('path');
var server = http.createServer(function (request, response) {
    var pathname = url.parse(request.url).pathname;
    console.log(pathname)
    var realPath = pathname.substr(1);//path.join("assets", 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 + ".");

但这个比较麻烦就是每次都要把这个文件copy到项目目录下,而且安装sass环境也是需要ruby的,所以我们有必要了解下gem安装sass 和asdf.

先导官网下载个ruby ,安装完之后就可以使用

gem install sass

命令安装其他组件了,比如要装sass环境.如果要安装beta版本的,可以在命令行中输入

gem install sass --pre

升级命令是

gem update sass

今天我们要装asdf这么个http服务,首先我们使用

gem install asdf

安装完成之后就可以在你需要访问的文件夹目录使用

asdf -p 8080

然后就会出现如下图所示:

然后你就可以使用 http://localhost:8080/ 来访问当前目录的http服务了。

由于国内网络原因,有可能导致 rubygems.org 存放在 Amazon S3 上面的资源文件间歇性连接失败。这时候我们可以通过gem sources命令来配置源,先移除默认的https://rubygems.org源,然后添加淘宝的源https://ruby.taobao.org/,然后查看下当前使用的源是哪个,如果是淘宝的,则表示可以输入sass安装命令gem install了。

$ gem sources --remove https://rubygems.org/
$ gem sources -a https://ruby.taobao.org/
$ gem sources -l
*** CURRENT SOURCES ***

https://ruby.taobao.org
# 请确保只有 ruby.taobao.org
$ gem install sass
原文地址:https://www.cnblogs.com/xiangbing/p/ruby.html