โก URL Shortener Microservice for freeCodeCamp
- Creates a url short ID and stores it in a database with the original url
- Adding the shortID in a url address will redirect to the original website
- This was part of the FreeCodeCamp exercises for Front End Certification
- Note: mLab MongoDB Heroku add-on has been shut down - suggest MongoDB Atlas (free tier) is used instead
- Note: to open web links in a new window use: ctrl+click on link
๐ Table of contents
๐ General info
- Original instructions (User Stories) from FCC:
1. I can POST a URL to [project_url]/api/shorturl/new
and I will receive a shortened URL in the JSON response. Example : {"original_url":"www.google.com","short_url":1}
2. If I pass an invalid URL that doesn't follow the valid http(s)://www.example.com(/more/routes)
format, the JSON response will contain an error like {"error":"invalid URL"}
. HINT: to be sure that the submitted url points to a valid site you can use the function dns.lookup(host, cb)
from the dns
core module.
3. When I visit the shortened URL, it will redirect me to my original link.
4. [this_project_url]/api/shorturl/3
will redirect to http://forum.freecodecamp.com
- MongoDB Cloud Atlas database set up to use Google Cloud Storage.
- Example: POST [project_url]/api/shorturl/new - body (urlencoded) : url=
https://www.google.com
๐ท Screenshots
๐ถ Technologies
- Node v12 javaScript runtime built on Chrome's V8 JavaScript engine
- Express v5 Fast, unopinionated, minimalist web framework for Node.js
- MongoDB Atlas cloud-based NoSQL database
- mongoose v5 object modelling for node.js.
- dbs v0.2.2 DNS Server with a web UI and Redis configuration store
- shortid v2 to generate short non-sequential url-friendly unique ids
- Cors v2 node.js package for providing Connect/Express middleware that can be used to enable CORS with various options.
๐พ Setup
- Create MongoDB Atlas Cloud database (or local installed MongoDB database) and add user access/database credentials (USER_NAME, USER_PASSWORD, DB_CLUSTER, PORT, DB_NAME & SESSION_SECRET) to a new
.env
file. These are used inserver.js
. - Add your IP address to MongoDB Atlas Network Access whitelist. Or simply whitelist all (IP address 0.0.0.0/0).
- Run
node server.js
for a dev server. Navigate tohttp://localhost:4000/
. - The app will not automatically reload if you change any of the source files.
๐ป Code Examples
- extract from
server.js
to check url is valid then generate short url using npm shortid module then save both in database
//use dns lookup function to check url is valid
dns.lookup(parsedURL.host, (err, address) => {
if (address === undefined) {
res.json({
error:
'This url failed the formatting test - check and try again. ' +
err,
});
} else {
const data = new shortURL({
originalURL: prependHttp(originalURL),
shortenedURL: shortid.generate(),
});
console.log(data);
data.save((err) => {
if (err) {
console.log(err);
return res.send('error: unable to save to database');
}
console.log('all OK');
});
res.send(data);
}
});
๐ Features
- Common MongoDB Atlas Cloud connection method used to save a lot of time with multiple projects.
๐ Status & To-Do List
- Status: Working. Nothing to stop the same url being saved in the database multiple times with different shortIDs.
- To-Do: add a check to prevent duplicate websites having different shortIDs in the database. Replace var with const & let
๐ Inspiration
๐ License
- This project is licensed under the terms of the MIT license.
โ๏ธ Contact
- Repo created by ABateman, email: [email protected]