小编Rar*_*rio的帖子

如何在 TypeScript 中使用 Jest 和 Knex 进行测试?

我正在尝试使用 Jest 和 Knex 测试 GraphQL 服务器。我很难弄清楚如何在打字稿中使用 knexfile。但是现在除了测试之外,开发和生产环境一切正常。

这是我的当前knexfile.ts

// knexfile.ts

const defaults = {
  client: 'pg',
  connection: {
    host: DB_HOST,
    user: DB_USER,
    password: DB_PASSWORD,
    database: DB_DATABASE
  },
  pool: {
    min: 2,
    max: 10
  },
  migrations: {
    extension: 'ts',
    directory: './migration',
    tableName: 'knex_migrations'
  },
  seeds: {
    extension: 'ts',
    directory: './seed'
  }
};

interface KnexConfig {
  [key: string]: object;
}

const knexConfig: KnexConfig = {
  local: {
    client: 'sqlite3',
    connection: {
      filename: './dev.sqlite3'
    }
  },

  development: {
    ...defaults,
    debug: …
Run Code Online (Sandbox Code Playgroud)

javascript node.js typescript knex.js jestjs

8
推荐指数
1
解决办法
2326
查看次数

如何保护 Next.js 中的 API 路由?

我正在使用 Next.js API 路由 - https://nextjs.org/docs/api-routes/introduction,但我不知道如何保护它们免受公众侵害。

现在这些路由在我的生产服务器中是公开的。

例如:当我mysite.com/api/cats在浏览器中访问时,它返回 -

{ success: true, data: [...] }
Run Code Online (Sandbox Code Playgroud)

请帮忙,如何向公众隐藏这些 API 路由?

javascript node.js next.js

8
推荐指数
1
解决办法
2万
查看次数

如何使用 Joi 最新版本验证枚举字符串?

如何验证枚举字符串?

我曾经按照这里的建议使用此方法:https : //github.com/hapijs/joi/issues/1449

enum UserRole {
  Admin = 'admin',
  Staff = 'staff'
}

const validator = {
  create: Joi.object().keys({
    first_name: Joi.string().min(1),
    last_name: Joi.string()
      .min(1)
      .required(),
    password: Joi.string()
      .regex(/^[\x20-\x7E]+$/)
      .min(8)
      .max(72)
      .required(),
    role: Joi.string()
      .valid([UserRole.Admin, UserRole.Staff])
      .optional(),
    is_active: Joi.boolean().optional()
  })
};
Run Code Online (Sandbox Code Playgroud)

但现在 , Error: Method no longer accepts array arguments: valid

javascript node.js express typescript

7
推荐指数
1
解决办法
5884
查看次数

如何在 GitHub 操作中为 Docker 构建和发布设置正确的工作目录?

我有这个 GitHub 操作作业来构建 Docker 映像并将其发布到 GitHub 注册表。

...
jobs:
  push_to_registry:
    name: Push Docker image to GitHub Packages
    runs-on: ubuntu-latest
    steps:
      - name: Check out the repo
        uses: actions/checkout@v2
      - name: Push to GitHub Packages
        uses: docker/build-push-action@v1
        with:
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
          dockerfile: Dockerfile
          registry: docker.pkg.github.com
          repository: myrepo/myimg
          tag_with_ref: true
Run Code Online (Sandbox Code Playgroud)

但是它在父目录中运行,而我Dockerfile的在里面app/

.
|- .github/workflow/ci.yaml
|- README
|- app/
   |- Dockerfile
   |- package.json
   |- package.lock.json
   |- node_modules/
   |- src/
   |- ...
Run Code Online (Sandbox Code Playgroud)

我尝试设置working-directory: …

github docker dockerfile github-actions

7
推荐指数
1
解决办法
9798
查看次数

如何在 terraform 中使用受密码保护的私有 ssh 密钥?

我正在按照本教程https://www.digitalocean.com/community/tutorials/how-to-use-ansible-with-terraform-for-configuration-management来学习 Terraform 和 Ansible。

当我执行时terraform apply,它抛出一个错误:

digitalocean_droplet.web[2]: Provisioning with 'remote-exec'...

Error: Failed to parse ssh private key: ssh: this private key is passphrase protected

Error: Error creating droplet: POST https://api.digitalocean.com/v2/droplets: 422 Failed to resolve VPC

  on droplets.tf line 1, in resource "digitalocean_droplet" "web":
   1: resource "digitalocean_droplet" "web" {
Run Code Online (Sandbox Code Playgroud)

这是代码:

  provisioner "remote-exec" {
    inline = ["sudo apt update", "sudo apt install python3 -y", "echo DONE!"]

    connection {
      host        = self.ipv4_address
      type        = "ssh"
      user        = "root"
      private_key …
Run Code Online (Sandbox Code Playgroud)

ssh ansible digital-ocean terraform devops

7
推荐指数
1
解决办法
4863
查看次数

在 Go 中使用 Gorm 返回一个数组

我正在使用 Node 和 Go 学习微服务。

我现在在使用 Gorm 从数据库 (postgres) 查询所有用户时遇到问题。

我通常这样查询以获取所有用户并且它有效:

    // Fetch connection and close db
    db := InitPg()
    defer db.Close()

    // Create an array of users to populate
    var users []*User
    db.Find(&users)

    // Successfully returns an array of users
    return users, nil
Run Code Online (Sandbox Code Playgroud)

但是现在有了生成的 protobuf,它会抱怨:

func (s *Server) Index(ctx context.Context, _ *shop.Empty) (*shop.IndexUserResponse, error) {
    // func (s *Server) Index(ctx context.Context, request *shop.Empty) error {
    db := InitPg()
    defer db.Close()

    // Confirmed created 2 users in database
    var users …
Run Code Online (Sandbox Code Playgroud)

postgresql go protocol-buffers grpc go-gorm

6
推荐指数
1
解决办法
3839
查看次数

如何使用 Sharp 调整 S3 中的图像大小?

我正在尝试使用 Node.js API 中的 @aws-sdk/v3 调整从 S3 上传的图像的大小。

首先,我从 S3 获取对象(图像),如下所示: https: //github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascriptv3/example_code/s3/src/s3_getobject.ts

...

  const params = { Bucket: AWS_BUCKET_NAME, Key: key }
  const data = await s3.send(new GetObjectCommand(params))
  console.log(` >> Success, bucket returned:`, data) // Log correctly
Run Code Online (Sandbox Code Playgroud)

然后我尝试调整数据(图像)的大小:

  const convert = sharp(data).resize(size, size).webp()
Run Code Online (Sandbox Code Playgroud)

但它抛出:

 >> data.type: object
(node:35583) UnhandledPromiseRejectionWarning: Error: Input file is missing
...
Run Code Online (Sandbox Code Playgroud)

而且我不知道该如何上传回来?

我究竟做错了什么?

javascript amazon-s3 amazon-web-services node.js sharp

6
推荐指数
1
解决办法
2973
查看次数

如何使用TypeORM创建关系数据?

我正在尝试创建并保存用户的产品。

这是我的尝试:

// userEntity.ts

import { Product } from '../product';

@Entity('user')
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({
    unique: true
  })
  email: string;

  @Column()
  password: string;

  @OneToMany(type => Product, product => product.user)
  @JoinTable()
  products: Product[];

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;
}
Run Code Online (Sandbox Code Playgroud)
// productEntity.ts

import { ProductCategory } from './productCategoryEntity';
import { User } from '../user';

@Entity('product')
export class Product {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @ManyToOne(type => ProductCategory, {
    eager: true,
    cascade: true
  })
  @JoinTable()
  category: ProductCategory; …
Run Code Online (Sandbox Code Playgroud)

node.js express typescript typeorm

5
推荐指数
0
解决办法
5931
查看次数

Apollo GraphQL 不断接收请求,没有进行任何查询或更改

我正在学习 GraphQL 并且即将完成本教程,这在以前从未发生过。

问题是在浏览器中打开 GraphQL Playground 后,GraphQL 服务器不断接收请求,即使没有进行查询或更改。

我看到服务器返回这些响应:

{
    "name":"deprecated",
    "description":"Marks an element of a GraphQL schema as no longer supported.",
    "locations":[
      "FIELD_DEFINITION",
      "ENUM_VALUE"
    ],
    "args":[
      {
          "name":"reason",
          "description":"Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted using the Markdown syntax (as specified by [CommonMark](https://commonmark.org/).",
          "type":{
            "kind":"SCALAR",
            "name":"String",
            "ofType":null
          },
          "defaultValue":"\"No longer supported\""
      }
    ]
}
Run Code Online (Sandbox Code Playgroud)

javascript node.js typescript apollo graphql

5
推荐指数
1
解决办法
1708
查看次数

如何检查 Vue/Nuxt 中是否加载了图像?

Vue 和 Nuxt 的新手。我试图在图像完全加载之前显示骨架。

这是我的尝试,骨架显示,但图像从未加载,并且 onImageLoad 从未被调用。

<template>
  <div>
    <img v-if="isLoaded" @load="onImgLoad" :src="me.img">
    <div v-else class="skeleton"></div>
  </div>
</template>

<script lang="ts">
export default {
  props: {
    me: Object,
  },
  data() {
    return {
      isLoaded: false,
    }
  },
  methods: {
    onImgLoad() {
      console.log(` >> isLoaded:`, this.isLoaded)
      return this.isLoaded = true
    },
  },
}
</script>
Run Code Online (Sandbox Code Playgroud)

我有一些损坏的图像 url 来测试后备 src,这是一个问题吗?但我尝试删除那些损坏的链接,但它仍然无法正常工作。

示例数据:

export const me = {
  name: 'David',
  img: 'https//david.png', // Example broken > https://no.jpg
},
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

javascript vue.js

5
推荐指数
1
解决办法
8597
查看次数