Closed saileshkush95 closed 1 month ago
I'll give you a sample code that you can follow my steps to modify the project.
1.Install the http server:
pnpm install @nestjs/platform-express
2.Modified src/main/index.ts
import { NestFactory } from '@nestjs/core'
import { app } from 'electron'
import type { MicroserviceOptions } from '@nestjs/microservices'
import { ElectronIpcTransport } from '@doubleshot/nest-electron'
import { AppModule } from './app.module'
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true'
async function electronAppInit() { const isDev = !app.isPackaged app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit() })
if (isDev) { if (process.platform === 'win32') { process.on('message', (data) => { if (data === 'graceful-exit') app.quit() }) } else { process.on('SIGTERM', () => { app.quit() }) } }
await app.whenReady() }
async function bootstrap() { try { await electronAppInit()
const nestApp = await NestFactory.create(AppModule)
nestApp.connectMicroservice<MicroserviceOptions>({
strategy: new ElectronIpcTransport('IpcTransport'),
})
nestApp.startAllMicroservices()
await nestApp.listen(3000)
} catch (error) { // eslint-disable-next-line no-console console.log(error) app.quit() } }
bootstrap()
- 3.Add http method decorators in controllers, `src/main/app.controller.ts`:
```ts
import { Controller, Get } from '@nestjs/common'
import { IpcHandle, Window } from '@doubleshot/nest-electron'
import { Payload } from '@nestjs/microservices'
import { type Observable, of } from 'rxjs'
import type { BrowserWindow } from 'electron'
import { AppService } from './app.service'
@Controller()
export class AppController {
constructor(
private readonly appService: AppService,
@Window() private readonly mainWin: BrowserWindow,
) { }
@IpcHandle('msg')
public handleSendMsg(@Payload() msg: string): Observable<string> {
const { webContents } = this.mainWin
webContents.send('reply-msg', 'this is msg from webContents.send')
return of(`The main process received your message: ${msg} at time: ${this.appService.getTime()}`)
}
@Get('/hello')
getHello(): number {
return this.appService.getTime()
}
}
Now you're ready to launch a hybrid app! nest.js offers a hybrid app pattern, and you can check out its documentation: Hybrid application
I want to create api in this project and Im using this approach but this is not working,
and Im trying to access api using this endpoint, http://localhost:5173/hello