Electronã§BrowserWindowã«localhostã表示ããã³ã¼ããæ¸ãã¾ããã
PCå
ã«æ¢ã«localhostãç«ã¡ä¸ãã£ã¦ããã°ããããã®ã¾ã¾ãç«ã¡ä¸ãã£ã¦ããªããã°èµ·åãã¦ããBrowserWindowã«è¡¨ç¤ºãã¾ãã
éçãªãµã¼ããèµ·åããã ããªã®ã§ãExpressã¯å¿
è¦ãªãã®ã§ããã楽ãããã®ã§Expressã使ã£ã¦ãã¾ãã
使ãé
ãã®ãããç¨ãªã±ã¼ã¹ã ã¨æãã¾ãããæ¢ã«å®æãã¦ããNext.jsã§ã®ããã¸ã§ã¯ãããNuxt.jsã§ã®ããã¸ã§ã¯ãããã¹ã¯ãããã¢ããªåãããå ´åã«å©ç¨ãã¦ãã¾ãã
éçæ¸ãåºããããã£ã¬ã¯ããªããã¹ãã£ã³ã°ããã ãã§OKã§ãã
ãã ãæè¿ã¯æ°è¦ã§Electronã¢ããªãä½ãéã¯ã Next.jsのElectronテンプレート ã使ã£ã¦ããã®ã§ãåºçªã¯æ¸ã£ã¦ãã¦ãã¾ãã
blog.kimizuka.org
blog.kimizuka.org
Next.jsã®Electronãã³ãã¬ã¼ãã¯æå¤ã¨ã¯ã¾ãã©ãããå¤ãã®ã§ãä¸æ¸¬ã®äºæ
ã«ç´é¢ããéã®ããã¯ã¢ãããã©ã³ã¨ãã¦ã¯å¿å¼·ãã§ãã
ã½ã¼ã¹ã³ã¼ã
const { app, BrowserWindow } = require('electron'); const express = require('express'); const path = require('path'); const request = require('request'); const DELAY = 500; request({ // ãªã¯ã¨ã¹ããéä¿¡ããlocalhost:3000ãç«ã¡ä¸ãã£ã¦ããã確èªãã url: 'http://localhost:3000', method: 'GET' }, (err) => { if (err && err.code === 'ECONNREFUSED') { const server = express(); const http = require('http').Server(server); server.use('/', express.static(`${ __dirname }/out`)); http.listen(3000, '0.0.0.0'); // ç«ã¡ä¸ãã£ã¦ããªãå ´åã¯localhost:3000ãèµ·åãã } }); app.on('ready', () => { setTimeout(() => { const window = new BrowserWindow(); window.loadURL('http://localhost:3000'); }, DELAY); }); app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit(); });