Skip to content

NodeJs Rest API using Clean Architecture, Typescript and Design Patterns

License

Notifications You must be signed in to change notification settings

rheimon1/url_shortener_api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

URL Shortener API

NodeJs Rest API using Clean Architecture, Typescript and Design Patterns

Instructions

  1. To configure database server address, url expiry time, url prefix and port the server will listen on. It is necessary to create an .env file at the root of the project and define the following environment variables:

Example:

MONGO_URL=mongodb://localhost:27017/url-shortener
BASE_URL_PREFIX=http://localhost:3333
PORT=3333
URL_EXPIRES_AT_IN_HOURS=24
  1. It is necessary to have at least version 16 of NodeJS, along with a package manager, such as Yarn or NPM, to proceed with the installation of the application;

  2. To proceed with the installation, just use the commands:

  • Using Yarn

    1. yarn
    2. yarn build
    3. yarn start
  • Using NPM

    1. npm install
    2. npm run build
    3. npm run start

After executing the commands successfully, the following message will appear in the terminal:

Server running at <BASE_URL_PREFIX> (url configured in environment variables in BASE_URL_PREFIX)

  1. With the application started, it is possible to communicate with the API through two endpoints:

Endpoints

Shorten Url

POST <BASE_URL_PREFIX>/shortenUrl

the <BASE_URL_PREFIX> is ​​equivalent to the server url defined in the environment variables

Body:

{
  "originalUrl" : string
}

The originalUrl attribute contains the URL to be shortened.

Response

{
  "success" : boolean
  "data": string
}

The success attribute indicates the url was shortened successfully or not.

The data attribute contains the shortened url.

Redirect Url

GET <BASE_URL_PREFIX>/:urlId

the <BASE_URL_PREFIX> is ​​equivalent to the server url defined in the environment variables

Params:

{
  "urlId" : string
}

The urlId attribute contains the URL shortened.

Response

Redirects to original url or returns 404 indicating URL not found

Status Codes

API returns the following status codes:

Status Code Description
200 OK
400 BAD REQUEST
404 NOT FOUND
500 INTERNAL SERVER ERROR
  1. Folder organization
  • Domain: Application domain, contains the use case contracts and models;
  • Data: Contains the implementation of the use cases and contracts for the dependencies of the use cases;
  • Infra: Infrastructure, contains the implementation of contracts defined in the Data layer and other external dependencies;
  • Main: Contains all files related to configuration, instantiation of all classes, definition of routes, middlewares, among others;
  • Presentation: Contains definition of API and endpoints, handling of HTTP requests and related contracts;
  • Validation: Contains the contracts and implementation of the validations used in the presentation layer, in the controllers.

About

NodeJs Rest API using Clean Architecture, Typescript and Design Patterns

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published