Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

# URL Shortener Backend

This project is a simple URL shortener service built with Node.js, Express, MongoDB, and EJS for server-side rendering.

## API Endpoints

### 1. Generate Short URL

- **Endpoint:** `POST /url/api/shorturl`
- **Request Body (JSON):**
  ```json
  {
    "url": "https://example.com"
  }
  ```
- **Response (on success):**
  - Renders the EJS `home` view with the generated short URL.
  - If using API (e.g., with Postman), you may receive:
    ```json
    {
      "id": "generatedShortId"
    }
    ```
- **Response (on error):**
  ```json
  {
    "msg": "Url was needed"
  }
  ```

---

### 2. Redirect to Original URL

- **Endpoint:** `GET /url/api/redirect/:shortId`
- **Path Parameter:**
  - `shortId`: The unique ID for the shortened URL.
- **Response:**
  - Redirects to the original URL.
  - If not found:
    ```json
    {
      "msg": "Short URL not found"
    }
    ```

---

### 3. Get URL Analysis

- **Endpoint:** `GET /url/api/url/anlaysis/:shortId`
- **Path Parameter:**
  - `shortId`: The unique ID for the shortened URL.
- **Response:**
  ```json
  {
    "totalclick": 5,
    "analysis": [
      { "timestamps": 1719320000000 },
      { "timestamps": 1719321000000 }
    ]
  }
  ```
  - `totalclick`: Number of times the short URL was visited.
  - `analysis`: Array of visit timestamps.

---

### 4. Home Page (EJS Render)

- **Endpoint:** `GET /api/home`
- **Response:** Renders the `home.ejs` view, displaying:
  - A form to submit a new URL.
  - A table of all shortened URLs with their shortId, original URL, and click count.

---

## EJS Usage

[EJS](https://ejs.co/) is used as the server-side templating engine for rendering dynamic HTML pages.  
- The main view is `view/home.ejs`.
- After generating a short URL or visiting the home page, the server renders this template, passing data such as the generated short URL or the list of all URLs.
- EJS allows embedding JavaScript logic and variables directly in the HTML, making it easy to display dynamic content.

## Running the Project

1. Install dependencies:
   ```sh
   npm install
   ```
2. Start the server:
   ```sh
   npm start
   ```
3. Visit [http://localhost:3000/api/home](http://localhost:3000/api/home) in your browser.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages