久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

NodeJS Electron 與 express

NodeJS Electron with express(NodeJS Electron 與 express)
本文介紹了NodeJS Electron 與 express的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試使用電子(用于網站和桌面應用程序)和 express(用于會話等)制作網絡應用程序

I'm trying to make a webapplication using electron (for the website and desktop application) and express (for sessions etc.)

現在,我把它作為我的 app.js:

Now, I got this as my app.js:

const express = require('express');
const {app, BrowserWindow} = require('electron');

exp = express();
exp.set('views', __dirname + '/views/');
exp.use(express.static(process.cwd() + '/views'));
exp.get('/', function(req, res) {
    res.render('index', {});
});

function onAppReady() 
{
    mainWindow = new BrowserWindow({
        width: 1080,
        height: 720,
        autoHideMenuBar: true,
        useContentSize: true,
        resizable: false
    });

    mainWindow.loadURL('http://localhost:5000/');
    mainWindow.focus();
    mainWindow.webContents.openDevTools();
}

app.on('ready', onAppReady);

現在,有幾個問題:

  1. 如果我使用 node app.js,我會收到此錯誤:
  1. If I use node app.js, I get this error:

Line: `app.on('ready', onAppReady);`

TypeError: Cannot read property 'on' of undefined
at Object.<anonymous> (/home/josh/chat_program/client/app.js:26:4)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:420:7)
at startup (bootstrap_node.js:139:9)
at bootstrap_node.js:535:3

  1. 如果我使用 electron .,應用程序會啟動,但我沒有收到請求或網頁.我得到的只是基本的 HTML,沒有任何東西(只有 doctype HTML HEAD 和 BODY).
  1. If I use electron ., the application starts, but I don't get either a request or a webpage. All I get is basic HTML without anything (only doctype HTML HEAD and BODY).

找了半天也沒找到.

推薦答案

兩件事.

首先我要澄清一下你的路徑設置和使用,更像這樣:

First I'd clarify your pathing setup and usage, more like this:

const publicPath = path.resolve(__dirname, '/views');
// point for static assets
app.use(express.static(publicPath));
//view engine setup
app.set('views', path.join(__dirname, '/views/'));

app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

其次,我會將我所有的 express 代碼包裝到一個文件中,該文件是一個自執行函數,因此它會在您需要時運行一次.比如我的快遞文件,我稱之為 app.js 文件:

Second, I would wrap all my express code into a single file that is a self-executing function, so it runs once when you require it. Such as my express file which I call my app.js file:

'use strict';
(function () {
    const express = require('express');
    const path = require('path');
    const logger = require('morgan');
    const cookieParser = require('cookie-parser');
    const bodyParser = require('body-parser');
    const routes = require('./routes.js');

    const app = express();
    const publicPath = path.resolve(__dirname, '../dist');
    const port = 3000;

    // point for static assets
    app.use(express.static(publicPath));

    //view engine setup
    app.set('views', path.join(__dirname, '../dist'));
    app.engine('html', require('ejs').renderFile);
    app.set('view engine', 'html');

    app.use(logger('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({
        extended:true
    }));

    app.use('/', routes);

    app.use(cookieParser());

    const server = app.listen(port, () => console.log(`Express server listening on port ${port}`));

    module.exports = app;

}());

然后在我的主文件(在我的例子中我稱之為 main.js 而不是 app.js)中,我將應用程序和 express 服務器實例化如下:

Then in my main file (which I call main.js not app.js, in my case), I instantiate the app and the express server as follows:

'use strict';
const app = require('electron').app;
const Window = require('electron').BrowserWindow; // jshint ignore:line
const Tray = require('electron').Tray; // jshint ignore:line
const Menu = require('electron').Menu; // jshint ignore:line
const fs = require('fs');

const server = require('./ServerSide/app');

let mainWindow = null;

app.on('ready', function () { 
    const path = require('path');
    const iconPath = path.resolve(__dirname, './dist/myicon.ico');
    const appIcon = new Tray(iconPath);
    mainWindow = new Window({
        width: 1280,
        height: 1024,
        autoHideMenuBar: false,
        useContentSize: true,
        resizable: true,
        icon: iconPath
        //  'node-integration': false // otherwise various client-side things may break
    });
    appIcon.setToolTip('My Cool App');
    mainWindow.loadURL('http://localhost:3000/');

    // remove this for production
    var template = [
        {
            label: 'View',
            submenu: [
                {
                    label: 'Reload',
                    accelerator: 'CmdOrCtrl+R',
                    click: function(item, focusedWindow) {
                        if (focusedWindow) {
                            focusedWindow.reload();
                        }
                    }
                },
                {
                    label: 'Toggle Full Screen',
                    accelerator: (function() {
                        if (process.platform === 'darwin') {
                            return 'Ctrl+Command+F';
                        } else {
                            return 'F11';
                        }
                    })(),
                    click: function(item, focusedWindow) {
                        if (focusedWindow) {
                            focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
                        }
                    }
                },
                {
                    label: 'Toggle Developer Tools',
                    accelerator: (function() {
                        if (process.platform === 'darwin') {
                            return 'Alt+Command+I';
                        } else {
                            return 'Ctrl+Shift+I';
                        }
                    })(),
                    click: function(item, focusedWindow) {
                        if (focusedWindow) {
                            focusedWindow.toggleDevTools();
                        }
                    }
                }
            ]
        }
    ];

    const menu = Menu.buildFromTemplate(template);
    Menu.setApplicationMenu(menu);

    mainWindow.focus();

});

// shut down all parts to app after windows all closed.
app.on('window-all-closed', function () {
    app.quit();
});

請注意,我在 Windows 平臺上成功使用了此功能,因此可能需要對本示例中列出的任何平臺特定項目進行小幅調整.

Note that I am using this with success on Windows platform, so small tweaks may be needed for any platform specific items listed in this example.

這篇關于NodeJS Electron 與 express的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 成人久久久 | 四虎精品在线 | 超碰超碰| 久草在线影 | 精品一区二区电影 | 欧美5区| 日韩在线视频一区 | 一级免费毛片 | 视频精品一区 | 亚洲成色777777在线观看影院 | 中文字幕99| 日韩在线不卡 | 国产欧美一区二区三区久久人妖 | 国产精品视频一二三 | 久久久一区二区三区 | 91大神新作在线观看 | 在线观看精品 | 亚洲第一视频网站 | 久久精品国产亚洲夜色av网站 | 国产一级大片 | 6080亚洲精品一区二区 | av在线播放免费 | 亚洲国产福利视频 | 国产精品欧美一区喷水 | 亚洲一区二区三区久久久 | 欧美aⅴ在线观看 | 永久免费av | 国产成人网| 国产精品久久网 | 欧美一区中文字幕 | 日韩欧美在线视频 | 福利网站导航 | 99re视频在线| 国产在线精品一区二区三区 | 最新av在线播放 | 欧美日韩福利 | 日韩中文字幕在线免费 | 狠狠天天| 免费午夜电影 | jav成人av免费播放 | 四虎影视免费在线 |