簡體   English   中英

在 firebase 雲函數中部署 nestjs

[英]Deploy nestjs in firebase cloud functions

我想用 ssr 部署 angular 應用程序。 我最近發現nestjs中有nestjs示意圖,可以自動添加 ssr 功能,但我沒有找到任何關於如何部署這個項目的教程或解釋,所以我可以獲得 ssr。

我的步驟是:

  1. 使用 cli 創建一個新的 angular 應用程序。
  2. 通過“ng add @nestjs/ng-universal”添加nestjs
  3. 使用 firebase cli 添加雲功能和托管
  4. 建造一切
  5. 我如何部署它,以便應用程序將位於雲功能上的托管和nestjs服務器上,並將被調用以進行預渲染。

你可以簡單地打firebase deploy ,一旦你得到了你的index.js文件中使用的WebPack transpiled。 同樣,您可以為此構建一個管道。

處理函數應如下所示:

import * as express from 'express';
import * as functions from 'firebase-functions';
import { AppModule } from './app.module';
import { Express } from 'express';
import { ExpressAdapter } from '@nestjs/platform-express';
import { NestFactory } from '@nestjs/core';

const server: Express = express();

// Create and init Nest server based on Express instance.
const createNestServer = async (expressInstance: Express) => {
  const app = await NestFactory.create(
    AppModule,
    new ExpressAdapter(expressInstance)
  );
  app.listen(4048);
};

createNestServer(server);
exports.angularUniversalFunction = functions.https.onRequest(server); // Export Firebase Cloud Functions to work on

正如您所寫的那樣,您一切正常,我假設您知道如何為 SSR 設置所有其他內容。 在其他情況下,請檢查此演示倉庫https://github.com/kamilmysliwiec/universal-nest

編輯 20-1-2020

基於@TayambaMwanza 的問題,我還添加了我的(與服務器相關的)webpack 配置:

/* Custom webpack server properties. */
const dotenv = require('dotenv-webpack');
const nodeExternals = require('webpack-node-externals');
const path = require('path');
const webpack = require('webpack');
const WebpackConfigFactory = require('@nestjs/ng-universal')
  .WebpackConfigFactory;

// Nest server's bundle for SSR.
const webpackConfig = WebpackConfigFactory.create(webpack, {
  server: './server/main.ts'
});

// Ignore all "node_modules" when making bundle on the server.
webpackConfig.externals = nodeExternals({
  // The whitelisted ones will be included in the bundle.
  whitelist: [/^ng-circle-progress/, /^ng2-tel-input/]
});

// Set up output folder.
webpackConfig.output = {
  filename: 'index.js', // Important in terms of Firebase Cloud Functions, because this is the default starting file to execute Cloud Functions.
  libraryTarget: 'umd', // Important in terms of Firebase Cloud Functions, because otherwise function can't be triggered in functions directory.
  path: path.join(__dirname, 'functions') // Output path.
};

// Define plugins.
webpackConfig.plugins = [
  new dotenv(), // Handle environemntal variables on localhost.
  // Fix WARNING "Critical dependency: the request of a dependency is an expression".
  new webpack.ContextReplacementPlugin(
    /(.+)?angular(\\|\/)core(.+)?/,
    path.join(__dirname, 'apps/MYPROJECT/src'), // Location of source files.
    {} // Map of routes.
  ),
  // Fix WARNING "Critical dependency: the request of a dependency is an expression".
  new webpack.ContextReplacementPlugin(
    /(.+)?express(\\|\/)(.+)?/,
    path.join(__dirname, 'apps/MYPROJECT/src'), // Location of source files.
    {}
  )
];

webpackConfig.target = 'node'; // It makes sure not to bundle built-in modules like "fs", "path", etc.

module.exports = webpackConfig; // Export all custom Webpack configs.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM