在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
由于在通常的前端开发情况下,我们会有可能需要一个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 --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
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论