balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

If I move app.js to a subfolder, "sails lift" works 100%, but "node ." gives blank 404 'Not found' page #6917

Open NathanHawks opened 4 years ago

NathanHawks commented 4 years ago

Node version: both 10.18.0 and 8.9.3 Sails version (sails): 1.2.3 ORM hook version (sails-hook-orm): ^2.1.1 Sockets hook version (sails-hook-sockets): 2.0.0 Organics hook version (sails-hook-organics): not in package-lock.json Grunt hook version (sails-hook-grunt): 4.0.1 Uploads hook version (sails-hook-uploads): not in package-lock.json DB adapter & version (e.g. sails-mysql@5.55.5): using builtin NeDB Skipper adapter & version (e.g. skipper-s3@5.55.5): not in package-lock.json


OS environment: Windows 8.1 64-bit

Before posting this, I verified: moved the app.js to the toplevel folder and changed package.json:main to reflect such. No issues there. So:

My sails app is also an electron app. Electron-packager requires your entry point be in a subfolder. So I move app.js to a subfolder and update my package.json. With this, my sails app still works 100% if I do a sails lift, but if I do node . (which is how electron prefers to do things), I get the following results:

I've thrown try/catch blocks at everything in app.js, and made all the electron stuff conditional, but that has had no effect nor revealed any extra information.

My project is open source, so I have no problem sharing code/configs as necessary to troubleshoot.

I realize this is not the electron.js support site, but node . is supposed to work and it normally does until I move the entry point... am I just unaware of a config step or something?

Thanks for your time.

sailsbot commented 4 years ago

@NathanHawks Thanks for posting! We'll take a look as soon as possible.

In the mean time, there are a few ways you can help speed things along:

Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.

For help with questions about Sails, click here.

NathanHawks commented 4 years ago

@johnabrams7 this is a bug unless your software is "working as intended" when its main entry point can't be run from a subfolder via node ..

NathanHawks commented 4 years ago

I found the obvious workaround does work:

  1. Move the "real" main entry script to the top-level folder of the project (same folder as package.json).
  2. Create a new script and put it in the subfolder, e.g. app/launcher.js. It only needs to contain one line: require('../app.js'); (presuming your "real" main entry script is app.js).
  3. Update package.json 's main node to point at app/launcher.js

I still consider this a bug and would be willing to follow troubleshooting instructions in order to get you the diagnostic data you need.

whichking commented 4 years ago

Hey, @NathanHawks! Glad you found a workaround. We were wondering if wrapping the whole Sails app in a subfolder might also solve this issue.

NathanHawks commented 4 years ago

Short answer, yes, at least with a fresh app.

Steps taken:

Note: the command did not cleanly exit on its own.

 info: Installing dependencies...
Press CTRL+C to cancel.
(to skip this step in the future, use --fast)
 info: Created a new Sails app `app`!

(steps continued)

const request = require('request');
// electron config stuff
var backgroundColor = '#1A1A1A';
var width = 800, height = 600;
// get electron
const electron = require('electron');
// prime electron app
const app = electron.app;
// try to prevent multiple instances of the app running
app.requestSingleInstanceLock();
// electron window(s)
var mainWindow = null;
// when sails says it's lifted, wait a delay or else JS & CSS return 404's
var windowCreationDelay = 5000;
// sails app address
const appAddress = 'http://127.0.0.1';
const appPort = 1337;

  // give sails time
  setTimeout(() => {
    try {
      // create the browser window
      if (app) {
                const BrowserWindow = electron.BrowserWindow;
        mainWindow = new BrowserWindow({show: false, width: width, height: height,
          backgroundColor: backgroundColor
        });
        // hide menu bar where available
        mainWindow.setMenuBarVisibility(false);
        // maximize the window
        mainWindow.maximize();
        // go to the sails app
        mainWindow.loadURL(`http://127.0.0.1:1337/`);
        // show javascript & DOM consoles
        mainWindow.webContents.openDevTools();
        // show browser only when it's ready to render itself
        mainWindow.once('ready-to-show', () => {
          mainWindow.show();
        });
        // setup close function
        mainWindow.on('closed', function() {
          mainWindow = null;
        });
      }
    }
    catch (e) { console.error(e); }
  }, windowCreationDelay);

// quit when all windows are closed
if (app) app.on('window-all-closed', function() {
  if (process.platform !== 'darwin') {
    sails.lower(() => {
      app.exit();
    });
  }
})

// --------------------------------------------------------------
// insert sails-generated app code here
// --------------------------------------------------------------
NathanHawks commented 4 years ago

Note that the app subfolder now has its own node_modules folder. Project size on disk is currently 1.3 GB after structuring things this way. This includes the electron-packager build. Compare to 1.1 GB, the size of an actual project (also with an electron-packager build in tow).

NathanHawks commented 4 years ago

(Apologies if there was any confusion ---- the title refers to running with node . but this was to simplify the question since it was a requirement of electron-packager)

It also works with node .