阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

使用Ruby搭建简易的HTTP服务和sass环境

125次阅读
没有评论

共计 2709 个字符,预计需要花费 7 分钟才能阅读完成。

由于在通常的前端开发情况下,我们会有可能需要一个 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

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

使用 Ruby 搭建简易的 HTTP 服务和 sass 环境

然后你就可以使用 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

Ruby 中的遍历指定目录的文件方法 http://www.linuxidc.com/Linux/2015-01/111525.htm

Ubuntu 下搭建 Ruby On Rails http://www.linuxidc.com/Linux/2012-06/61981.htm

实测 Ubuntu 13.10 上搭建 Ruby on Rails http://www.linuxidc.com/Linux/2014-02/96399.htm

Ruby on Rails 4 Tutorial 中文版 高清完整 PDF http://www.linuxidc.com/Linux/2014-04/100253.htm

Ruby 2.2 的增量垃圾收集机制 http://www.linuxidc.com/Linux/2015-06/119329.htm

Ruby 的详细介绍 :请点这里
Ruby 的下载地址 :请点这里

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2015-09/123515.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计2709字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中