Skip to content

Commit

Permalink
add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
shobhamrj committed May 21, 2023
1 parent 4cb415f commit be5eae1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 94 deletions.
106 changes: 12 additions & 94 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,106 +1,24 @@
<!--
title: 'Serverless Framework Node Express API on AWS'
description: 'This template demonstrates how to develop and deploy a simple Node Express API running on AWS Lambda using the traditional Serverless Framework.'
title: 'url-hasher'
description: 'Used to hashed URL'
layout: Doc
framework: v3
platform: AWS
language: nodeJS
priority: 1
authorLink: 'https://github.com/serverless'
authorName: 'Serverless, inc.'
authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4'
authorLink: 'https://github.com/shobhamrj'
authorName: 'Shobham Rajak'
-->

# Serverless Framework Node Express API on AWS
# url-hasher Build With Node Express API on serverless framework that leverage serverless framework to manage AWS services.
The API is implemented using Node.js, Express.js, and MongoDB with the intention of creating a URL hashing service. Additionally, the serverless framework is utilized, leveraging AWS services for deployment and scalability.

This template demonstrates how to develop and deploy a simple Node Express API service running on AWS Lambda using the traditional Serverless Framework.
Architecture Choice and Reasoning:

## Anatomy of the template
1. Node.js and Express.js: Node.js is a JavaScript runtime environment that allows for efficient and scalable server-side applications. Express.js is a popular web application framework for Node.js, providing a robust set of features and simplifying the development process. The choice of Node.js and Express.js enables efficient handling of incoming requests and seamless integration with other modules and libraries.

This template configures a single function, `api`, which is responsible for handling all incoming requests thanks to the `httpApi` event. To learn more about `httpApi` event configuration options, please refer to [httpApi event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api/). As the event is configured in a way to accept all incoming requests, `express` framework is responsible for routing and handling requests internally. Implementation takes advantage of `serverless-http` package, which allows you to wrap existing `express` applications. To learn more about `serverless-http`, please refer to corresponding [GitHub repository](https://github.com/dougmoscrop/serverless-http).
2. Serverless Framework: The serverless framework is utilized to deploy the application and manage AWS services. Serverless architecture allows for automatic scaling and efficient resource allocation, as the infrastructure is provisioned on-demand. It eliminates the need to manage servers and infrastructure manually, making it easier to focus on application development and reducing operational overhead.

## Usage
3. AWS Services: The architecture leverages AWS services to provide a scalable and reliable infrastructure. While the specific services used are not explicitly mentioned, AWS services commonly employed in serverless architectures include AWS Lambda for running serverless functions, Amazon API Gateway for managing API endpoints. The choice of AWS services ensures high availability, scalability, and fault tolerance for the application.

### Deployment
4. MongoDB: MongoDB is chosen as the database for storing URL data. MongoDB is a popular NoSQL database that offers flexibility and scalability, making it suitable for handling large amounts of data. It allows for easy storage and retrieval of JSON-like documents, which aligns well with the nature of the data being stored in the URL hashing service. Additionally, MongoDB's schema-less design enables agile development.

Install dependencies with:

```
npm install
```

and then deploy with:

```
serverless deploy
```

After running deploy, you should see output similar to:

```bash
Deploying aws-node-express-api-project to stage dev (us-east-1)

✔ Service deployed to stack aws-node-express-api-project-dev (196s)

endpoint: ANY - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com
functions:
api: aws-node-express-api-project-dev-api (766 kB)
```

_Note_: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to [`httpApi` event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api/).

### Invocation

After successful deployment, you can call the created application via HTTP:

```bash
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
```

Which should result in the following response:

```
{"message":"Hello from root!"}
```

Calling the `/hello` path with:

```bash
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/hello
```

Should result in the following response:

```bash
{"message":"Hello from path!"}
```

If you try to invoke a path or method that does not have a configured handler, e.g. with:

```bash
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/nonexistent
```

You should receive the following response:

```bash
{"error":"Not Found"}
```

### Local development

It is also possible to emulate API Gateway and Lambda locally by using `serverless-offline` plugin. In order to do that, execute the following command:

```bash
serverless plugin install -n serverless-offline
```

It will add the `serverless-offline` plugin to `devDependencies` in `package.json` file as well as will add it to `plugins` in `serverless.yml`.

After installation, you can start local emulation with:

```
serverless offline
```

To learn more about the capabilities of `serverless-offline`, please refer to its [GitHub repository](https://github.com/dherault/serverless-offline).
Overall, this architecture choice combines the flexibility of Node.js and Express.js, the scalability and ease of deployment offered by the serverless framework and AWS services, and the efficient data storage provided by MongoDB. It aims to create a high-performance, scalable, and reliable URL shortening service that can handle a large number of requests while minimizing operational complexity.
3 changes: 3 additions & 0 deletions assumptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Assumptions for this application
1. Url should be valid.
2. Url should be in the form https://xxxxxxxx/xxx/ or https://xxxxxxx.com. (full length URL)
43 changes: 43 additions & 0 deletions setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Steps to deploy the application via serverless framework / run it locally, run the test suits and access the api-docs

1. Install serverless framework, with npm globally
```
npm i serverless -g
```
2. Configure your AWS credentials to allow deployment using the Serverless Framework. You can follow the official Serverless Framework documentation. (https://www.serverless.com/framework/docs/providers/aws/guide/credentials/)

3. Set up a MongoDB instance or use an existing one to store the URL data. Make sure you have the connection details (e.g., URL, credentials) for your MongoDB database. (https://www.mongodb.com/atlas/database)

4. Clone the repository.
```
git clone [email protected]:shobhamrj/url-hasher.git
```
5. Change the directory and install the dependencies.
```
cd url-hasher
npm i
```

To deploy the application, serverless uses AWS Cloudformation to access the AWS resources therefore make a user AWS IAM and give these two permissions AdministratorAccess and AWSCloudFormationFullAccess, then run the command given below, (although you may adjust the permissions as needed)
```
serverless deploy
```
To delete the AWS resources, use this command
```
serverless remove
```


To run the application in your local machine make sure you have installed serverless-offline from npm (although it is already mentioned in package.json), then run the following command,
```
serverless offline
```

To run the test suits, use the command,
```
npm run test
```

The swagger api-docs is available in the home directory or '/api-docs'


4 changes: 4 additions & 0 deletions todo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Some Todo's for this application

1. Can be upgraded to UTM (Urchin Tracking Module). It is a snippet of text added to the end of a URL to track the metrics and performance of a specific digital marketing campaign. UTM codes can contain up to five parameters: Campaign, source, medium, content, and term.
2. AWS DynamoDB can also be used as database.

0 comments on commit be5eae1

Please sign in to comment.