Nest: how to make nestjs work with electron application

Created on 1 Nov 2018  ·  7Comments  ·  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ] Bug report
[*] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

the electron main.ts

import { app, BrowserWindow } from "electron";
import url from "url";
import { join } from "path";
import { spawn } from "child_process";
import { bootstrap } from "./src/server";

// Keep a global reference of the window object, if you don"t, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win!: any;

function createWindow() {
    // 创建浏览器窗口。
    win = new BrowserWindow({
        width: 1300,
        height: 900,
        minWidth: 1300,
        minHeight: 900,
    });

    // auth?: string;
    // hash?: string;
    // host?: string;
    // hostname?: string;
    // href?: string;
    // path?: string;
    // pathname?: string;
    // protocol?: string;
    // search?: string;
    // slashes?: boolean;
    if (process.env.NODE_ENV !== "production") {
        // 打开开发者工具
        win.webContents.openDevTools();
        // 然后加载应用的 index.html。
        win.loadURL(url.format({
            host: `127.0.0.1:${3000}`,
            slashes: true,
            protocol: "http",
        }));
    } else {
        win.loadFile(join(__dirname, "/index.html"));
    }




    // 当 window 被关闭,这个事件会被触发。
    win.on("closed", () => {
        // 取消引用 window 对象,如果你的应用支持多窗口的话,
        // 通常会把多个 window 对象存放在一个数组里面,
        // 与此同时,你应该删除相应的元素。
        win = null;
    });
}

// Electron 会在初始化后并准备
// 创建浏览器窗口时,调用这个函数。
// 部分 API 在 ready 事件触发后才能使用。
app.on("ready", async () => {
    createWindow();
    await bootstrap();
});

// 当全部窗口关闭时退出。
app.on("window-all-closed", () => {
    // 在 macOS 上,除非用户用 Cmd + Q 确定地退出,
    // 否则绝大部分应用及其菜单栏会保持激活。
    if (process.platform !== "darwin") {
        app.quit();
    }
});

app.on("activate", async () => {
    // 在macOS上,当单击dock图标并且没有其他窗口打开时,
    // 通常在应用程序中重新创建一个窗口。
    if (win === null) {
        createWindow();
        await bootstrap();
    }
});

// 在这个文件中,你可以续写应用剩下主进程代码。
// 也可以拆分成几个文件,然后用 require 导入。

the nestjs app entry

import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { IoAdapter } from "@nestjs/websockets";
import { HttpExceptionFilter } from "@utils/http.exception";
import { Interceptor } from "@utils/interceptor";



export async function bootstrap() {
    const app = await NestFactory.create(AppModule);
    // app.useWebSocketAdapter(new IoAdapter());
    app.setGlobalPrefix("/client/api/v1");
    app.useGlobalFilters(new HttpExceptionFilter());
    app.useGlobalInterceptors(new Interceptor());
    await app.listen(1992);
}

Current behavior


When I exec nestjs inside electron.
like below image
image

just init database
the url can't init and the server can't lanuch.

Expected behavior


I hope the nestjs normal.

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


Nest version:core 5.3.10


For Tooling issues:
- Node version: 8.12.0  
- Platform:  linux 

Others:

my application architecture
image

All 7 comments

@kamilmysliwiec please help me

You can't use NestJS inside an Electron app or anything server related, even if you could or were to do so, you shouldn't.
You need to run a standalone nodejs execution outside the Electron app to start up a server.

If you just need the architecture you could use https://github.com/marcus-sa/one as a replacement.

May be you can tell me the reason that can't use nest inside electron.
thank you @marcus-sa

I just told you why.
Electron isn't a framework for building server-side apps, but for building cross-platform desktop apps.

A progressive Node.js framework for building efficient and scalable server-side applications on top of TypeScript & JavaScript (ES6, ES7, ES8) heavily inspired by Angular 😻🚀 https://nestjs.com/

@marcus-sa thank you

@printjs Actually, the new angular-console electron app uses nestjs for its backing server. You can see that they are starting the nestjs server here on lines 7 and 57. Viola.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

2233322 picture 2233322  ·  3Comments

rlesniak picture rlesniak  ·  3Comments

rafal-rudnicki picture rafal-rudnicki  ·  3Comments

menme95 picture menme95  ·  3Comments

VRspace4 picture VRspace4  ·  3Comments