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

Created on 23 Dec 2019  路  7Comments  路  Source: balderdashy/sails

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. [email protected])_: using builtin NeDB
Skipper adapter & version _(e.g. [email protected])_: 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:

  • Sails does lift in the console, but first it warns me about a missing session secret.
  • In the browser, I get a 404 code and a plaintext 'Not found'.

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.

helpful info or workaround question

All 7 comments

@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:

  • look for a workaround. _(Even if it's just temporary, sharing your solution can save someone else a lot of time and effort.)_
  • tell us why this issue is important to you and your team. What are you trying to accomplish? _(Submissions with a little bit of human context tend to be easier to understand and faster to resolve.)_
  • make sure you've provided clear instructions on how to reproduce the bug from a clean install.
  • double-check that you've provided all of the requested version and dependency information. _(Some of this info might seem irrelevant at first, like which database adapter you're using, but we ask that you include it anyway. Oftentimes an issue is caused by a confluence of unexpected factors, and it can save everybody a ton of time to know all the details up front.)_
  • read the code of conduct.
  • if appropriate, ask your business to sponsor your issue. _(Open source is our passion, and our core maintainers volunteer many of their nights and weekends working on Sails. But you only get so many nights and weekends in life, and stuff gets done a lot faster when you can work on it during normal daylight hours.)_
  • let us know if you are using a 3rd party plugin; whether that's a database adapter, a non-standard view engine, or any other dependency maintained by someone other than our core team. _(Besides the name of the 3rd party package, it helps to include the exact version you're using. If you're unsure, check out this list of all the core packages we maintain.)_

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.

@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 ..

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.

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

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

Steps taken:

  • mkdir subfolder; cd subfolder
  • npm init (with app/app.js as main entry point)
  • mkdir app; cd app
  • sails new . and chose Web App

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)

  • Confirm the app works in a regular browser
  • Quick 'n dirty integrate Electron into app.js:
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
// --------------------------------------------------------------

  • cd ..; electron . -- confirmed boilerplate app works
  • Copied relevant deps/devdeps/config from another project to package.json
  • npm install
  • electron-packager . testSubfolder --platform=win32 --arch=x64
  • Lower antivirus shields (or add a security exception)
  • Run the new EXE

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).

(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 .

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mahfuzur picture mahfuzur  路  3Comments

thomasfr picture thomasfr  路  3Comments

kesavkolla picture kesavkolla  路  4Comments

3imed-jaberi picture 3imed-jaberi  路  3Comments

Alirezamohammadi picture Alirezamohammadi  路  4Comments