在Firebase云功能中部署NestJS

Lag*_*tos 5 firebase google-cloud-functions angular nestjs

我想用ssr部署angular应用程序。我最近发现,有一些nestjs角度图可以自动添加ssr功能,但我没有找到有关如何部署此项目的任何教程或说明,因此可以得到ssr。

我的步骤是:

  1. 使用cli创建一个新的角度应用程序。
  2. 通过“ ng add @ nestjs / ng-universal添加nestjs
  3. 添加云功能并使用Firebase CLI托管
  4. 建立一切
  5. 如何部署此应用程序,以便该应用程序将位于托管nestjs功能上,而服务器将位于云功能上,并将被调用以进行预渲染。

Dan*_*cki 5

firebase deploy一旦你index.js使用 webpack 转译了你的文件,你就可以简单地点击。同样,您可以为此构建一个管道。

处理函数应如下所示:

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
Run Code Online (Sandbox Code Playgroud)

正如您所写,您一切正常,我假设您知道如何为 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.
Run Code Online (Sandbox Code Playgroud)