tauri-apps / tauri

Build smaller, faster, and more secure desktop and mobile applications with a web frontend.
https://tauri.app
Apache License 2.0
83.39k stars 2.51k forks source link

How to run nodejs server in build mode ? #1773

Closed madhupalo closed 3 years ago

madhupalo commented 3 years ago

First of all this is a great initiative and love ❤️ the project. I have an express app with front end in Vue and backend in nodejs server. I want to run and package both of them and have the api server run with a port.

Is this possible ?

Thanks again

lucasfernog commented 3 years ago

Check out the sidecar documentation and maybe the guijs project as an example (it is the same setup as you have). Please reach out on Discord if you have any questions.

nothingismagick commented 3 years ago

Also, maybe consider if you ABSOLUTELY need the express server. Is this just for one device, or are you making it available over the network? Or is it because its something you are used to doing? I am asking, because this will add bloat and sacrifice performance (and probably have a security impact too)...

madhupalo commented 3 years ago

Hi @nothingismagick - Its a developer tool which would require to be run have local server and can be run local as well as remote (when i say remote meaning the user data is all in same machine while we have user identity in cloud). The base app needs to have local server exposed and is part of product to be used by the user.

I dont want to use electron.

lucasfernog commented 3 years ago

This might help you:

https://tauri.studio/en/docs/guides/bundler/sidecar

This is how GUIJS does it: https://github.com/Akryum/guijs/blob/40051bf07a2b9dc8dd0be19d15de93a84bed726c/packages/%40guijs/tauri-app/src-tauri/tauri.conf.json#L25 https://github.com/Akryum/guijs/tree/feat/tauri-beta-rc/packages/%40guijs/tauri-app/src-tauri/bin https://github.com/Akryum/guijs/blob/40051bf07a2b9dc8dd0be19d15de93a84bed726c/packages/%40guijs/tauri-app/src-tauri/src/main.rs#L186

madhupalo commented 3 years ago

Hi @lucasfernog Yes I am trying your repo to make it work but facing issues. I will check again, also want to use latest of Tauri while the repo uses previous version

lucasfernog commented 3 years ago

I'll update that branch to beta right now.

lucasfernog commented 3 years ago

tauri-theia also uses the sidecar API.

lucasfernog commented 3 years ago

I've updated guijs to beta without any issues: https://github.com/Akryum/guijs/pull/94/commits/eb002f588ebc8db64e196d9a67f20b70638d260f

madhupalo commented 3 years ago

Hi @lucasfernog

In Cargo I see its taking the source from github, is that an issue ?

https://github.com/Akryum/guijs/blob/40051bf07a2b9dc8dd0be19d15de93a84bed726c/packages/%40guijs/tauri-app/src-tauri/Cargo.toml

lucasfernog commented 3 years ago

Check out the latest commit on the branch feat/tauri-beta-rc, it pulls the latest beta release.

zzl221000 commented 3 years ago

@lucasfernog How to close the external bin when tauri exits

nothingismagick commented 3 years ago

This all sounds very much to me like we have a candidate for a tutorial.

madhupalo commented 3 years ago

Hi @nothingismagick - yes definitely and all the work @lucasfernog has done could be of great use. I could do a PR but I am still facing issues with unable to start server with specific port.

nothingismagick commented 3 years ago

yeah, the problem with starting on a specific port is that how do you know if that port is not already in use?

madhupalo commented 3 years ago

We could try using https://www.npmjs.com/package/get-port which will generate a random port if the specified port is not used and retry till we get an open one

rxliuli commented 3 years ago

If there is a simpler way to use nodejs, it will make it easier for nodejs services to migrate to tauri

chrisspiegl commented 3 years ago

I just got interested in Tauri as an alternative to Electron. I find the size benefits incredible, however, I would love to see an easier way to have a Node.JS backend. I understand the preference for rust, yet I am not willing to learn that environment at this time. So I am now subscribed to this issue here and am looking forward to see how things continue.

I tried understanding the ways guijs does it but that's a whole bunch of packages and files to go through which feels overkill.

Maybe someone could create a boilerplate with an express.js server? Or possibly a tutorial?

Thank you and I am looking forward to learn more.

Laegel commented 3 years ago

Hey @chrisspiegl and @rxliuli , You'll find an up to date guide right here to embed a binary such as Node.js. Note that you may need to use Tauri's dev branch in order to make it work.

chrisspiegl commented 3 years ago

@Laegel I am trying and I think I am getting closer. Thanks for the tip. I am however, now stuck at one specific point which is that upon running the app I get:

thread 'tokio-runtime-worker' panicked at 'Failed to start connectthemall server: Io(Os { code: 2, kind: NotFound, message: "No such file or directory" })', src/main.rs:78:12

The line that throws the error is:

let (mut rx, mut child) = Command::new_sidecar("connectthemallserver")
  .expect("failed to setup `connectthemall` sidecar")
  .spawn()
  .expect("Failed to start connectthemall server");

The files should be present, and the binary of the build is also as big as the binary I am packaging.

The files also are named with the tripplet stuff.

Maybe this is the thing you mentioned about the dev branch? But how do I use the dev branch? I am already running the published beta that is available on NPM?

UPDATE

I found out that the dev branch thing is probably done through the Cargo.toml file and by editign the tauri in the [dependencies] line like so:

[dependencies]
tauri = { git = "https://github.com/tauri-apps/tauri", branch = "dev", features = ["shell-execute"] }

This however did not fix the roadblock I have encountered with the Sidecar not being found 😢 .

lemarier commented 3 years ago

Could you try to put the sidecar in src-tauri/target/debug (same folder as the binary)

rxliuli commented 3 years ago

But I found another lower-level library webview, which can display the specified url as a window, combined with pkg seems to be easier to package the nodejs service into a binary program

chrisspiegl commented 3 years ago

@lemarier That indeed elevated the problem and the app launched without the error message that the sidecar was not found. A bit of light at in the tunnel.

Do you have any idea about how this now could be adjusted to work for builds and dev as well? Am I doing something wrong or can I adjust my code somehow?

And @rxliuli, that is an idea. But I like that tauri has more to offer (menu bar icon, menu customization, and more). If it would not be for the crazy size and stuff I'd use electron but I'd prefer to ship sub 100MB — I am ok with not being in the sub 1MB range 🙈

lemarier commented 3 years ago

@lemarier That indeed elevated the problem and the app launched without the error message that the sidecar was not found. A bit of light at in the tunnel.

awesome!

Do you have any idea about how this now could be adjusted to work for builds and dev as well? Am I doing something wrong or can I adjust my code somehow?

I'll ask @lucasfernog to help on this as I'm not 100% sure what's the best way to do it.

I'd prefer to ship sub 100MB — I am ok with not being in the sub 1MB range

What's about deno? You can do a deno compile to get a binary with deno + your code compiled so instead of shipping node binary you ship your deno bundle that include the v8 engine. (that's a personal suggestion)

rxliuli commented 3 years ago

What's about deno? You can do a deno compile to get a binary with deno + your code compiled so instead of shipping node binary you ship your deno bundle that include the v8 engine. (that's a personal suggestion)

This reminds me of an article. . . https://hackernoon.com/how-it-feels-to-learn-javascript-in-2016-d3a717dd577f#.5zzf9ydlm

lucasfernog commented 3 years ago

Sidecar on dev was fixed and will be available on the next release.

chrisspiegl commented 3 years ago

@lucasfernog That sounds awesome, is there a timeline? For now, copying the binary will work for me during development.

lucasfernog commented 3 years ago

Next week probably.

nothingismagick commented 3 years ago

But I found another lower-level library webview, which can display the specified url as a window, combined with pkg seems to be easier to package the nodejs service into a binary program

Yes, we were involved in webview for quite some time, but its C approach proved to cumbersome for our project

chrisspiegl commented 3 years ago

@lemarier and @rxliuli in terms of the deno compile idea. That may be something for others to work on. But for me this is a side project not full time work. I don't want to learn tones of new things if I can avoid it. And learning a new eco system (aka deno or rust) and becoming fluent in it is not my goal here.

I am very much looking forward to being able to program web view based applications with the knowledge I have though. So I am pumped for the possibilities of what Tauri will offer me in this regard.

NitinRamnani commented 3 years ago

Hi @chrisspiegl were you able to use Node server as sidecar and did server started when you launched the app. I'm also trying to build an app with Vue & Node and have added node server in sidecar but it doesn't starts when I launch tauri app.

chrisspiegl commented 3 years ago

Hi @NitinRamnani, I tried for quite some time and I think I got it working in some setup prototype way but at the end decided that (for now) I am not going to package my app with any UI builder. The effort was just not worth it at this point of development.

Personally, I got mostly stuck with the whole "rust + node.js + etc" situation. I found Tauri as a Electron alternative but with the whole Rust backend it makes it too complicated for me to get my head into all that since I want to have a environment where JavaScript is my language and I don't want to learn Rust for anything right now 🙈.

So, TLDR:

I am not using this in production right now and also have not really found a good solution to even get it up and running which is reliable.

One thing I can say is that I started building my app into a binary with CAXA which seemed to have worked pretty well.

xeoshow commented 2 years ago

Hello,

Just would like to know, for the time being, any conclusion or best solution for tauri + nodejs + vue?

Thanks a lot.

Best regards, Jason