Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/farazf19/url-shortener-nodejs
URL Shortener: This project is a simple URL shortener service.
https://github.com/farazf19/url-shortener-nodejs
Last synced: about 2 months ago
JSON representation
URL Shortener: This project is a simple URL shortener service.
- Host: GitHub
- URL: https://github.com/farazf19/url-shortener-nodejs
- Owner: FarazF19
- Created: 2024-07-18T11:01:29.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-07-20T08:29:54.000Z (5 months ago)
- Last Synced: 2024-07-20T09:38:44.074Z (5 months ago)
- Language: JavaScript
- Homepage:
- Size: 3.4 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# URL Shortener
This project is a simple URL shortener service, similar to bit.ly, where you provide a long URL and receive a shortened URL in return. The backend is built using Node.js and MongoDB for storing the URLs and their visit histories and createdBy attribute. Also added static pages using EJS Server side rendering for homepage,signup and login. For authentication, statefull authenticaation using cookies and session id were also used.
## Features
- Shorten a given long URL.
- Redirect to the original URL using the shortened URL.
- Track visit history with timestamps.## Endpoints
### Create Short URL
- **POST /url**
- Creates a shortened URL for the provided long URL.### Redirect to Original URL
- **GET /:shortId**
- Redirects to the original URL using the provided short ID and logs the visit.### Returns the analytics
- **GET /analytics/:shortId**
- Returns the analytics and totalclicks of URL using the provided short ID .## Getting Started
### Prerequisites
- Node.js
- MongoDB
- npm### Installing
1. Clone the repository
```bash
git clone https://github.com/yourusername/url-shortener.git
```
````2. Navigate to the project directory
```bash
cd url-shortener
```
3. Install dependencies
```bash
npm install
```### Running the Server
1. Start MongoDB on your local machine or connect to a MongoDB instance.
2. Run the server
```bash
npm start
```
3. The server will start on `http://localhost:8001`.### Example Code
```javascript
const express = require("express");
const urlRoute = require("./routes/url");
const { connectMongoDB } = require("./connect");
const URL = require("./models/url.models");
const app = express();const PORT = 8001;
// MongoDB connect
connectMongoDB("mongodb://localhost:27017/short-url").then(() =>
console.log("MongoDB connected")
);app.use(express.json());
// Routes
app.use("/url", urlRoute);app.get("/:shortId", async (req, res) => {
const shortId = req.params.shortId;
const entry = await URL.findOneAndUpdate(
{ shortId },
{
$push: {
visitHistory: { timestamp: Date.now() },
},
}
);
res.redirect(entry.redirectURL);
});app.listen(PORT, () => console.log("Server Started at PORT:", PORT));
```## Authors
- Faraz
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Acknowledgements
Special thanks to Piyush Garg for his YouTube tutorial series on Node.js which was instrumental in learning the concepts applied in this project. Check out his channel: [Piyush Garg YouTube Channel](https://www.youtube.com/watch?v=4WvX9dBjiJo&list=PLinedj3B30sDby4Al-i13hQJGQoRQDfPo&index=21)
```
```