Created
May 20, 2015 07:23
-
-
Save bellbind/6ae79ebef25504107650 to your computer and use it in GitHub Desktop.
[electron]Use electron as a Web Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html><head><script src="app.js"></script></head><body></body></html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// utility | |
var console = { | |
log: function () { | |
var ipc = require("ipc"); | |
var args = ["console"].concat([].slice.call(arguments)); | |
return ipc.sendSync.apply(ipc, args)[0]; | |
} | |
}; | |
var quit = function () { | |
var ipc = require("ipc"); | |
return ipc.sendSync("app", "quit")[0]; | |
}; | |
// server handler | |
window.addEventListener("load", function () { | |
var ipc = require("ipc"); | |
ipc.on("request", function (req, port) { | |
//console.log(req); | |
var doc = document.implementation.createHTMLDocument(req.url); | |
var h1 = doc.createElement("h1"); | |
h1.textContent = "Hello DOM: " + req.url; | |
doc.body.appendChild(h1); | |
ipc.send(port, 200, {"content-type": "text/html;charset=UTF-8"}, | |
doc.documentElement.outerHTML); | |
}); | |
}, false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// electron main | |
console.log(process.versions); | |
var app = require("app"); | |
app.on("ready", function () { | |
var ipc = require("ipc"); | |
ipc.on("console", function (ev) { | |
var args = [].slice.call(arguments, 1); | |
var r = console.log.apply(console, args); | |
ev.returnValue = [r]; | |
}); | |
ipc.on("app", function (ev, msg) { | |
var args = [].slice.call(arguments, 2); | |
ev.returnValue = [app[msg].apply(app, args)]; | |
}); | |
var BrowserWindow = require("browser-window"); | |
var window = new BrowserWindow({show: false}); | |
window.loadUrl("file://" + __dirname + "/app.html"); | |
window.webContents.once("did-finish-load", function () { | |
var http = require("http"); | |
var crypto = require("crypto"); | |
var server = http.createServer(function (req, res) { | |
var port = crypto.randomBytes(16).toString("hex"); | |
ipc.once(port, function (ev, status, head, body) { | |
//console.log(status, head, body); | |
res.writeHead(status, head); | |
res.end(body); | |
}); | |
window.webContents.send("request", req, port); | |
}); | |
server.listen(8000); | |
console.log("http://localhost:8000/"); | |
}); | |
}); |
This gist is outdated. I've put up an up-to-date version here: https://gist.github.com/derekchiang/a38b72878d79d1fe4e19eb032ff2b505
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I got an error as below:
notes:
var ipc = require("ipc");
byvar ipc = require('electron').ipcRenderer
var ipc = require("ipc");
byvar ipc = require('electron').ipcMain
var app = require("app");
byvar app = require('electron').app
var BrowserWindow = require("browser-window")
byvar BrowserWindow = require('electron').BrowserWindow
my package.json file is:
{ "name": "electron_server", "description":"test", "repository": { "type": "git", "url": "" }, "license":"MIT", "version": "0.1.0", "main": "server.js", "scripts": { "start": "electron server.js", "build": "electron-packager . myApp --platform=win32 --arch=x64 --version=1.3.4 --out=Workspace --overwrite --icon=./icons/icon.icns", "package": "asar pack Workspace/myApp-win32-x64/resources/app Workspace/myApp-win32-x64/resources/app.asar", "setup": "node installer" }, "devDependencies": { "electron": "^1.3.4", "electron-packager": "^7.7.0", "electron-winstaller": "^2.3.4" } }