兩件事。
首先我想澄清你的尋路設置和使用,更是這樣的:
var 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');
其次,我想包我所有的快件代碼到一個單一的文件,該文件是一個自動執行的功能,所以它運行一次當你需要它。比如我的快遞文件,我打電話給我的app.js
文件:
(function() {
'use strict';
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes.js');
var app = express();
var publicPath = path.resolve(__dirname, '../dist');
var 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());
var server = app.listen(port, function() {
console.log('Express server listening on port ' + server.address().port);
});
module.exports = app;
}());
然後在我的主文件(我稱之爲main.js
不app.js,在我的情況),我實例化應用程序和Express服務器,如下所示:因此,可能需要
var app = require('electron').app;
var Window = require('electron').BrowserWindow; // jshint ignore:line
var Tray = require('electron').Tray; // jshint ignore:line
var Menu = require('electron').Menu; // jshint ignore:line
var fs = require('fs');
var server = require('./ServerSide/app');
var mainWindow = null;
app.on('ready', function() {
'use strict';
var path = require('path');
var 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();
}
}
},
]
}
];
var 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() {
'use strict';
app.quit();
});
請注意,我用這與在Windows平臺上的成功,小的調整在這個例子中列出的任何平臺的具體項目。
你需要分開你後端和前端部分 – MysterX
mmh你的例子在這裏工作沒有問題...(電子) –
是的,它的工作原理,但它沒有給我一個請求。 –