Tips Develop Serverless Apps with Node.js and S3

[TIẾNG VIỆT]:
** Phát triển các ứng dụng không có máy chủ với Node.js và S3 **

Máy tính không có máy chủ là một mô hình điện toán đám mây cho phép các nhà phát triển xây dựng và chạy các ứng dụng mà không phải lo lắng về việc quản lý máy chủ.Điều này có thể tiết kiệm thời gian và tiền bạc, và nó cũng có thể giúp việc mở rộng các ứng dụng dễ dàng hơn khi cần thiết.

Node.js là môi trường thời gian chạy JavaScript có thể được sử dụng để xây dựng cả các ứng dụng phía máy chủ và phía máy khách.Đây là một lựa chọn phổ biến để phát triển các ứng dụng không có máy chủ vì nó nhanh, có thể mở rộng và hiệu quả.

Amazon Simple Storage Service (S3) là một dịch vụ lưu trữ đối tượng dựa trên đám mây cung cấp một cách đơn giản và hiệu quả về chi phí để lưu trữ dữ liệu.Đây là một lựa chọn phổ biến để lưu trữ dữ liệu cho các ứng dụng không có máy chủ vì nó đáng tin cậy, có thể mở rộng và bảo mật.

Trong bài viết này, chúng tôi sẽ chỉ cho bạn cách phát triển một ứng dụng không có máy chủ với Node.js và S3.Chúng tôi sẽ tạo một ứng dụng đơn giản cho phép người dùng tải tệp lên S3.

## Điều kiện tiên quyết

Để làm theo với hướng dẫn này, bạn sẽ cần những điều sau đây:

* A [Node.js] (Node.js) cài đặt
* An [AWS] (Cloud Computing Services - Amazon Web Services (AWS)) Tài khoản
* [AWS CLI] (Install or update the latest version of the AWS CLI - AWS Command Line Interface)

## Tạo một ứng dụng không có máy chủ

Chúng tôi sẽ sử dụng [Framework không có máy chủ] (Serverless: Develop & Monitor Apps On AWS Lambda) để tạo ứng dụng không có máy chủ của chúng tôi.Khung không có máy chủ là một công cụ giúp dễ dàng xây dựng và triển khai các ứng dụng không có máy chủ.

Để tạo một ứng dụng mới, hãy chạy lệnh sau:

`` `
$ serverless Tạo--Template AWS-Nodejs
`` `

Điều này sẽ tạo ra một thư mục mới gọi là `my-app`.Thư mục sẽ chứa tệp `serverless.yml`, là tệp cấu hình cho ứng dụng.

## Thêm phụ thuộc S3

Chúng tôi cần thêm phụ thuộc S3 vào ứng dụng của chúng tôi.Để thực hiện việc này, hãy mở tệp `serverless.yml` và thêm các phụ thuộc sau:

`` `
Sự phụ thuộc:
- S3
`` `

Chúng tôi cũng cần thêm một thùng S3 vào ứng dụng của chúng tôi.Để thực hiện việc này, hãy chạy lệnh sau:

`` `
$ serverless xô tạo
`` `

Điều này sẽ tạo ra một thùng S3 mới được gọi là `My-app-bucket`.

## Viết mã ứng dụng

Chúng tôi cần viết mã cho ứng dụng của chúng tôi.Mã sẽ nằm trong một tệp có tên là `index.js`.

`` `
const AWS = Yêu cầu ('AWS-SDK');
const s3 = new Aws.s3 ();

exports.handler = async (sự kiện, bối cảnh) => {
const file = event.file;

const params = {
Xô: Process.Env.S3_Bucket,
Khóa: File.Name,
Body: File.Data,
};

đang chờ s3.putObject (params) .promise ();

trở lại {
Mã trạng thái: 200,
Body: Json.Stringify ({tin nhắn: 'Tệp được tải lên thành công'}),
};
};
`` `

Mã này sẽ tải tệp lên thùng S3.

## Triển khai ứng dụng

Để triển khai ứng dụng, hãy chạy lệnh sau:

`` `
$ Máy chủ không triển khai
`` `

Điều này sẽ triển khai ứng dụng cho AWS.

## Kiểm tra ứng dụng

Chúng tôi có thể kiểm tra ứng dụng bằng cách tải một tệp lên thùng S3.Để thực hiện việc này, hãy chạy lệnh sau:

`` `
$ curl -x put -f [email protected] https://s3.amazonaws.com/my-app-bucket/file.txt
`` `

Điều này sẽ tải lên tệp `file.txt` lên thùng S3.

Sau đó, chúng ta có thể xác minh rằng tệp đã được tải lên bằng cách chạy lệnh sau:

`` `
$ aws s3 ls s3: // my-app-bucket/
`` `

Điều này sẽ liệt kê nội dung của thùng S3.Tệp `file.txt` nên được liệt kê.

## Phần kết luận

[ENGLISH]:
**Develop Serverless Apps with Node.js and S3**

Serverless computing is a cloud computing paradigm that allows developers to build and run applications without having to worry about managing servers. This can save time and money, and it can also make it easier to scale applications as needed.

Node.js is a JavaScript runtime environment that can be used to build both server-side and client-side applications. It is a popular choice for developing serverless applications because it is fast, scalable, and efficient.

Amazon Simple Storage Service (S3) is a cloud-based object storage service that provides a simple and cost-effective way to store data. It is a popular choice for storing data for serverless applications because it is reliable, scalable, and secure.

In this article, we will show you how to develop a serverless app with Node.js and S3. We will create a simple app that allows users to upload files to S3.

## Prerequisites

To follow along with this tutorial, you will need the following:

* A [Node.js](https://nodejs.org/en/) installation
* An [AWS](https://aws.amazon.com/) account
* The [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html)

## Create a Serverless App

We will use the [Serverless Framework](https://www.serverless.com/) to create our serverless app. The Serverless Framework is a tool that makes it easy to build and deploy serverless applications.

To create a new app, run the following command:

```
$ serverless create --template aws-nodejs
```

This will create a new directory called `my-app`. The directory will contain a `serverless.yml` file, which is the configuration file for the app.

## Add S3 Dependencies

We need to add S3 dependencies to our app. To do this, open the `serverless.yml` file and add the following dependencies:

```
dependencies:
- s3
```

We also need to add an S3 bucket to our app. To do this, run the following command:

```
$ serverless bucket create
```

This will create a new S3 bucket called `my-app-bucket`.

## Write the App Code

We need to write the code for our app. The code will be in a file called `index.js`.

```
const AWS = require('aws-sdk');
const s3 = new AWS.S3();

exports.handler = async (event, context) => {
const file = event.file;

const params = {
Bucket: process.env.S3_BUCKET,
Key: file.name,
Body: file.data,
};

await s3.putObject(params).promise();

return {
statusCode: 200,
body: JSON.stringify({ message: 'File uploaded successfully' }),
};
};
```

This code will upload the file to the S3 bucket.

## Deploy the App

To deploy the app, run the following command:

```
$ serverless deploy
```

This will deploy the app to AWS.

## Test the App

We can test the app by uploading a file to the S3 bucket. To do this, run the following command:

```
$ curl -X PUT -F [email protected] https://s3.amazonaws.com/my-app-bucket/file.txt
```

This will upload the file `file.txt` to the S3 bucket.

We can then verify that the file was uploaded by running the following command:

```
$ aws s3 ls s3://my-app-bucket/
```

This will list the contents of the S3 bucket. The file `file.txt` should be listed.

## Conclusion
 
Back
Top