Distribute Node.js application as single cross-platform executable - node.js

I have a web app where the front and back ends are both written in Node. The backend needs to run on (recent) Windows, OSX, and Linux. The front end, obviously, will run in a (modern) web browser, with pages served out from the backend.
For ease of deployment/installation, I would like to provide an executable file that installs "the right way" for each of those platforms. In browsing StackOverflow, I found a couple candidates:
1) I see that Electron helps make native apps that run cross platform, but I haven't seen anything that describes how Electron could listen on (say) port 80 and serve out the desired set of pages.
Is using Electron a rational strategy? Can anyone give me pointers to projects that work like this?
2) I also see that pkg (https://github.com/zeit/pkg) claims to produce cross-platform binaries. Can anyone give their experience with it?
3) Any other alternatives?
Free/Open-source somewat preferred, but commercial packages are a possibility. Many thanks!

#saille seems to have nailed it. Searches for "electron and express" on https://discuss.atom.io/ yield multiple articles for the combination.
pkg will also support an Express backend server.
So I do not plan to search for alternatives, but will investigate whether Electron or pkg work best for me.

Related

Single executable for React App and Express API

I have a React App and a Express API. I want to package those two components into one single executable. Is there a way to do this? I don’t want a solution to my problem I want a hint into the right direction if this is possible.
I believe what you mean is not to keep the bundler running as well as the express server, unfortunately that's not possible if you're in developer mode (and) you're expecting realtime updates in your browser, but if you were in production, then it's not even the case that you need to run your bundler, cause your main.bundle.js is already built and ready.
I think this is what you are looking for. https://electronjs.org/
Electron or similar libraries help you to create an executable application which can be installed an run like a desktop application.
The only point you have to keep in mind is for accessing the database you will have to create a REST API and communicate via that.
Link for a simple tutorial.

Package node.js app as cross-platform executable, not for desktop app

There are a lot of questions on this topic, but they don't seem to distinguish between executables for desktop or server-side apps. I suppose my first question would be: what's the difference? For example, Zeit/pkg says they are a "node.js binary compiler", whereas nwjs (formerly node-webkit) says they are a "an app runtime based on Chromium and node.js".
I tried zeit/pkg and it works great, but have read that there can be performance issues unless it's configured properly. I wanted to make sure I was choosing the right tool and came across nwjs. It seems to do a lot of the same stuff pkg does, but has a larger following, as well as more docs and a robust api. Can I use nwjs as a server-side executable (i.e. not using the desktop feature) the same way I would use pkg?
This answer states that nwjs "is an option, but it really isn't set-up to do a server - client type relationship", but then a comment says "you can launch a server from node-webkit just in the way you launch it in Node.js. It's just that node-webkit provide another way beyond B/S architecture".
So, is nwjs effectively the same as pkg, or fundamentally different?
I realize that there's also Electron, which states "build cross platform desktop apps" and appears similar to nwjs. I'm not trying to get into a Electron vs nwjs debate, but rather desktop vs. server, if there's a difference.
you got most things already, only few clarifications are needed. Reason nw.js / Electron declares itself as for desktop application is, it's core architectural design is intended to integrate node.js with chromium to have UI enables create application does have UI. You can still use part of those framework (node.js side) without initiating visible ui, in that case behaviorwise it'll be similar to plain node.js does. Still there is caveat, like as it tightly integrated with chromium in core already for some cases you should have screen to chromium correctly initiates (or creating virtual buffer as lot of CI does, or etcs).
Also, when your concern is performance, I'd doubt using UI framework for server side work achieves what you desire - while there won't be huge, integration between node to chromium have overhead compare to bare node.js obvioulsy.
Getting back to original question, I feel question itself is somewhat vague. If the intention is truly server side application probably you won't need to package it but correctly deploy node and its dependency modules or packaging it sort of installable manner instead of creating single binary as pkg does.

Lightweight Desktop APP with Node - is that possible?

My current App (WPF in Visual Studio) weighs 2MB.
And now I want to re-create this App with Node and Javascript.
Why? Primary reason is - I want to learn some Javascript (yes, this is My way to learning and please dont give me advices how to learn JS). Secondary reason - I want to use full power of CSS3, while WPF App supports only CSS2.
The problem is Weight. Any example App builded with Electron or NWJS weighs about 115MB!!!
I can understand that Desktop App require Node and Browser to work, but 115MB?!
So my question is - Is there any way to create Lightweight Desktop App with Electron/NWJS (or similar alternative)?
I can accept final weight about 50-60MB.
Electron or NW.js are huge in terms of size and required memory, but there are lightweight alternatives: Libui-node and Positron. Other lightweight platforms not based on node.js are: React-Native, XULRunner and Qt Quick with QML.
I can only speak for Electron, but 115MB sounds about right for a minimal app, the Chromium content library which Electron is based on is an all or nothing sort of thing and takes up a large chunk of that space.

Desktop applications with Meteor.js

The options I've found for creating desktop applications in HTML, CSS, and JavaScript are:
Electron (formerly Atom-Shell)
NW.js (formerly Node-Webkit)
However, there doesn't seem to be any clear implementation for using meteor.js with these.
NW.js
I've seen some other questions of people asking this, but no implementation. Can't even figure out by the answers what I should be trying to put together:
Meteor leaderboard app on node-webkit
Demeteorizer with node-webkit
How can I start a Meteor instance before launching a node-webkit?
This guys actually got it running but doesn't say how: https://stackoverflow.com/questions/25508737/iron-router-routing-fails-on-reload-in-node-webkit
The idea proposed here is interesting—loading the desktop application completely from the web. However, I would like to allow my users to use the application offline.
Electron
There is an actual implementation, meteor-desktop-app, but it has no clear support for Windows. However, the atom-shell does support Windows just as NW.js does.
Has anyone figured out an implementation to allow meteor.js to operate as a desktop application?
If you drop the code from meteor-desktop-app into Electron Starter, you'll get Windows + Mac + Linux for free, as well as a build system you can definitely use for Real Apps.
Check out mongoclient's scaffold directory, it'll lead you to your electron app if you follow the steps from readme.

How to replace IIS with Node.js for simple websites

I am coming from Microsoft world so please bear with me on this. I was told I could install node.js and use that as a web server instead of IIS. This is a very small business application. In IIS I can create virtual directory and point to the location of the web page and everything works just fine. Based on very little I read, I have few questions;
Is it possible to run node js as a windows service or any other form so that it runs for ever? I did find the forever package that I think I can use.
In IIS, I can create virtual directory set the port and thats it, I have myself a website.
I do not see any examples where I can use a directory where I have a web page, written in java script and point it to run as a web site. All the examples have some thing like server.js and that runs and routes the call. what is the other way to host web sites and use node.js to simple run as a fast web server.
I was told I could install node.js and use that as a web server instead of IIS.
This is true, but as you already found out then you are in charge of providing for things that IIS was already doing for you (e.g. automatically restart on reboot, or on crashes, hosting multiple sites by creating virtual folders, et cetera.)
You can indeed get all of these things worked out in Node.js and there are several libraries that help on each of these areas. It's not too hard but you'll need to do a bit of researching.
You can also run Node.js behind IIS. Take a look at iisnode http://tomasz.janczuk.org/2011/08/hosting-nodejs-applications-in-iis-on.html
Is it possible to run node js as a windows service or any other form so
that it runs for ever?
The library Forever takes care of restarting the site when it crashes...but I don't know if you can run it as a Windows Service. I haven't tried that.
In IIS, I can create virtual directory set the port and thats it,
I have myself a website.
I assume you are talking about a site that serves static HTML files, right? If that's the case that's very easy to support in Node.js either writing your own web server or using Express.js to serve static files.
I do not see any examples where I can use a directory where I have a web page,
written in java script and point it to run as a web site. All the examples
have some thing like server.js and that runs and routes the call.
Here is an extremely simple example to serve plain HTML files in Node.js https://gist.github.com/2573391 Don't use this in production, though. It's just an example and it does not have any kind of error handling or security.
what is the other way to host web sites and use node.js to simple run
as a fast web server.
As others have said, you should look into Express.js http://expressjs.com/ It provides some of the infrastructure that you are very likely going to need when building traditional web sites.
You say you're running a "very small business application" behind IIS. Unless it's written for Node.js (in JavaScript), it won't work.
There are no examples pointing to a directory and running that as a website, because that's not how things are done in Node.js. You write a Node.js-application and pull in a webserver-library.
Put simply, In Node.js, you don't embed the appliation in the webserver; you embed the webserver in the application.
When I used node.js, I redirected HTTP requests by a proxy server, nginx. I don’t know if you can directly bind node.js as an HTTP server, but for what’s it worth, nginx is pretty nice!
First things first, allow me to share an introduction. IMHO you should take this decision ( of moving from IIS to nodeJS) by adding various parameters. I belong to the Java & PHP community yet I use NodeJS to achieve extremely specific implementation where NodeJS perform the fastest ( fast IO, AJAX-JSON responses & more ). As you are coming with a Microsoft background you should bare with less comfortable solutions.
Yes, its possible to run NodeJs as a windows service and Forever will do fine.
and yes you can create "Virtual Directories" but by creating symbolic links to each of your customer's web site.
I recommend to take a good look at bouncy & express, If you're willing to take this step then these packages is just what you need.
Cheers!

Resources