Skip to content

Instantly share code, notes, and snippets.

@bellbind
Created May 20, 2015 07:23
Show Gist options
  • Save bellbind/6ae79ebef25504107650 to your computer and use it in GitHub Desktop.
Save bellbind/6ae79ebef25504107650 to your computer and use it in GitHub Desktop.
[electron]Use electron as a Web Server
<!doctype html>
<html><head><script src="app.js"></script></head><body></body></html>
// 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);
// 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/");
});
});
@bellbind
Copy link
Author

run electron server.js (or run with xvfb-run as xvfb-run electron server.js on headless linux)

then access http://localhost:8000/foo/bar

@nevf
Copy link

nevf commented Apr 26, 2016

I'm new to Electron and just tried your sample app and I'm getting "waiting for localhost" which never ends and:

{ http_parser: '2.6.2',
  node: '5.10.0',
  v8: '4.9.385.28',
  uv: '1.8.0',
  zlib: '1.2.8',
  ares: '1.10.1-DEV',
  modules: '47',
  openssl: '1.0.2g',
  electron: '0.37.7',
  'atom-shell': '0.37.7',
  chrome: '49.0.2623.75' }
(electron) ipc module is deprecated. Use require("electron").ipcMain instead.
(electron) loadUrl is deprecated. Use loadURL instead.

Any suggestions?

Copy link

ghost commented Aug 19, 2016

@nevf The error and the way to fix it is clearly mentioned in the message:

(electron) ipc module is deprecated. Use require("electron").ipcMain instead.
(electron) loadUrl is deprecated. Use loadURL instead.

@hasan3ysf
Copy link

hasan3ysf commented Aug 30, 2016

I got an error as below:

notes:

  1. in the app.js, I replaced var ipc = require("ipc"); by var ipc = require('electron').ipcRenderer
  2. in the server.js, I replaced var ipc = require("ipc"); by var ipc = require('electron').ipcMain
  3. in the server.js, I replaced var app = require("app"); by var app = require('electron').app
  4. in the server.js, I replaced var BrowserWindow = require("browser-window") by var 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" } }

image

@derekchiang
Copy link

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