npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@thinkboat/serverless-nestjs

v1.0.0

Published

腾讯云 serverless components nestjs 组件

Downloads

48

Readme

Serverless Nest.js

腾讯云 Nest.js 组件 ⎯⎯⎯ 通过使用 Serverless Framework,基于云上 Serverless 服务(如网关、云函数等),实现“0”配置,便捷开发,极速部署你的 Nest.js 应用。

注意:本项目仅支持使用 ExpressAdapter 的 Nest.js 项目。

安装

通过 npm 安装最新版本的 Serverless Framework

$ npm install -g serverless

创建

通过如下命令和模板链接,快速创建一个 Nest.js 应用:

$ npm i -g @nestjs/cli
$ nest new serverless-nestjs
$ cd serverless-nestjs

执行如下命令,安装应用的对应依赖

$ npm install

项目改造

  1. 由于云端函数执行时不需要监听端口的,所以我们需要修改下入口文件 src/main.ts,如下:
import { NestFactory } from '@nestjs/core'
import { join } from 'path'
import { AppModule } from './app.module'

async function bootstrap() {
  const app = await NestFactory.create(AppModule)

  return app
}

// 注意: 通过注入 NODE_ENV 为 local,来方便本地启动服务,进行开发调试
const isLocal = process.env.NODE_ENV === 'local'
if (isLocal) {
  bootstrap().then((app) => {
    app.listen(3000, () => {
      console.log(`Server start on http://localhost:3000`)
    })
  })
}

// 导出启动函数,给 sls.js 使用
export { bootstrap }
  1. 项目根目录下新增 sls.js 文件,用来提供给 Nest.js 组件使用:
// 注意: 根据实际项目入口文件名进行修改,此 demo 为默认情况
const { bootstrap } = require('./dist/main')

module.exports = bootstrap

注意:实际开发可根据个人项目路径,来导出启动函数 bootstrap

部署

在项目根目录下创建serverless.yml文件,填写基本配置:

# severless.yml

component: nestjs # (必填) 组件名称,在该项目中为nestjs.
name: nestjsDemo # (必填) 组件实例名称.

# 组件配置参数,不填写则使用默认配置
inputs:
  src:
    src: ./
    exclude:
      - .env

由于 Nest.js 项目是 TypeScript,部署前需要编译成 JavaScript 后再部署:

$ npm run build
$ sls deploy

第一次部署可能耗时相对较久,但后续的二次部署会在几秒钟之内完成。部署完毕后,你可以在命令行的输出中查看到你 nestjs 应用的 URL 地址,点击地址即可访问你的 Nest.js 项目。

注意:

1、如您的账号未 登录注册 腾讯云,您可以直接通过 微信 扫描命令行中的二维码进行授权登陆和注册。

2、如果希望查看更多部署过程的信息,可以通过sls deploy --debug 命令查看部署过程中的实时日志信息,slsserverless 命令的缩写。

配置

nestjs 组件支持 0 配置部署,也就是可以直接通过配置文件中的默认值进行部署。但你依然可以修改更多可选配置来进一步开发该 nestjs 项目。

以下是 nestjs 组件的 serverless.yml 简单配置示例:

# serverless.yml

#应用配置
app: appDemo # (可选) 应用名称,默认为与组件实例名称
stage: dev # (可选)  用于开发环境的隔离,默认为 dev

#组件配置
component: nestjs # (必填) 组件名称,在该项目中为nestjs.
name: nestjsDemo # (必填) 组件实例名称.

#组件参数
inputs:
  src:
    src: ./ # (可选) 代码路径
    exclude:
      - .env
  functionName: nestjsDemo
  region: ap-guangzhou
  runtime: Nodejs10.15
  apigatewayConf:
    protocols:
      - http
      - https
    environment: release

点此查看全量配置及配置说明

当你根据该配置文件更新配置字段后,再次运行 serverless deploy 或者 serverless 就可以更新配置到云端。

远程调试云函数

部署了 nestjs.js 应用后,可以通过开发调试能力对该项目进行二次开发,从而开发一个生产应用。在本地修改和更新代码后,不需要每次都运行 serverless deploy 命令来反复部署。你可以直接通过 serverless dev 命令对本地代码的改动进行检测和自动上传。

可以通过在 serverless.yml文件所在的目录下运行 serverless dev 命令开启开发调试能力。

serverless dev 同时支持实时输出云端日志,每次部署完毕后,对项目进行访问,即可在命令行中实时输出调用日志,便于查看业务情况和排障。

除了实时日志输出之外,针对 Node.js 应用,当前也支持云端调试能力。在开启 serverless dev 命令之后,将会自动监听远端端口,并将函数的超时时间临时配置为 900s。此时你可以通过访问 chrome://inspect/#devices 查找远端的调试路径,并直接对云端代码进行断点等调试。在调试模式结束后,需要再次部署从而将代码更新并将超时时间设置为原来的值。详情参考开发模式和云端调试

查看

serverless.yml文件所在的目录下,通过如下命令查看部署状态:

$ serverless info

移除

serverless.yml文件所在的目录下,通过以下命令移除部署的 nestjs 服务。移除后该组件会对应删除云上部署时所创建的所有相关资源。

$ serverless remove

和部署类似,支持通过 sls remove --debug 命令查看移除过程中的实时日志信息,slsserverless 命令的缩写。

账号配置

当前默认支持 CLI 扫描二维码登录,如您希望配置持久的环境变量/秘钥信息,也可以本地创建 .env 文件

$ touch .env # 腾讯云的配置信息

.env 文件中配置腾讯云的 SecretId 和 SecretKey 信息并保存

如果没有腾讯云账号,可以在此注册新账号

如果已有腾讯云账号,可以在API 密钥管理中获取 SecretIdSecretKey.

# .env
TENCENT_SECRET_ID=123
TENCENT_SECRET_KEY=123

License

MIT License

Copyright (c) 2020 Tencent Cloud, Inc.