標籤:node-webkit
Node-webkit是一個基於Chromium與node.js的應用程式運行器,允許開發人員使用web技術編寫案頭應用。使用web技術開發,支援node.js,可相容多平台(window/mac/linux) 。
項目地址:https://github.com/rogerwang/node-webkit
下載:http://dl.node-webkit.org/
基本操作(window系統):
1、 下載系統對應的node-webkit版本,運行目錄中的nw.exe,顯示說明可以正常運行。
2、 建立package.json和index.html,壓縮成test.zip檔案包,直接拖到test.zip包到nw.exe以nw.exe方式開啟。
3、 把text.zip和nw.exe打包成test.exe,window下可能過命令
copy /b nw.exe+test.ziptest.exe
現在直接運行test.exe即可開啟。
注意:nw.exe必須放在+號前面,合并命令需用cmd運行執行,win8下powershell執行報錯“copy-Item:找不到接受實際參數‘b.exe’的位置形式參數”。
4、 安裝Enigma Virtual Box(http://enigmaprotector.com/en/aboutvb.html),打包所有檔案為一個可執行exe程式。
package.json:
Package.json為項目的設定檔,可配置視窗邊框、工具列、是否全屏、開啟時視窗大小及位置、表徵圖、node.js開機檔案、預設開啟頁面、視窗最大及最小尺寸等。預設版面設定main。參數詳細說明見:
https://github.com/rogerwang/node-webkit/wiki/Manifest-format
node.js模組擴充:
可在目錄中下載nodejs的各種模組擴充功能,存放在目錄node_modules。
在頁面中調用模組形式:
var imageMin= require('imagemin');//to do something…
Node.js內建伺服器:
參考:http://www.infoq.com/cn/news/2011/11/tyq-nodejs-static-file-server/
demo:http://download.csdn.net/detail/jyy_12/7924781
http.js:
var PORT = 8000;var http = require('http');var url = require('url');var path = require('path');var fs = require('fs');var mime = require('./mime').types;var server = http.createServer(function (request, response) { var pathname = url.parse(request.url).pathname; var realPath = "test" + pathname;var ext = path.extname(realPath);ext = ext ? ext.slice(1) : 'unknown';var contentType = mime[ext] || "text/plain"; path.exists(realPath, function (exists) { if (!exists) { response.writeHead(404, { 'Content-Type': contentType }); 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': contentType }); response.end(err); } else { response.writeHead(200, { 'Content-Type': contentType }); response.write(file, "binary"); response.end(); } }); } });});server.listen(PORT);console.log("Server runing at port: " + PORT + ".");
package.json:
{ "name": "nw-demo", "main": "http://localhost:8000/test.html", "nodejs":true, "node-main":"http.js", "window":{"width":400,"height":300,"transparent":true }}
Mime.js:
exports.types = { "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"};<strong></strong>
node-webkit缺點:
最終打包後的檔案較大,單純node-webkit所需的檔案就占將近60M,壓縮後也將近25M。
參考資料:
官方API:https://github.com/rogerwang/node-webkit/wiki
推薦(比較詳細的中文教程):http://www.cnblogs.com/xuanhun/tag/node-webkit/
http://pan.baidu.com/share/link?shareid=3743096074&uk=2754670725
http://damoqiongqiu.iteye.com/blog/2010720
http://www.baidufe.com/item/1fd388d6246c29c1368c.html
node-webkit執行個體:
https://github.com/zcbenz/nw-sample-apps
https://github.com/rogerwang/node-webkit/wiki/List-of-apps-and-companies-using-node-webkit
node-webkit讓web技術編寫案頭應用