Giter Club home page Giter Club logo

url-shorten-microservice's Introduction

โšก 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

GitHub repo size GitHub pull requests GitHub Repo stars GitHub last commit

๐Ÿ“„ 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

Example screenshot. Example screenshot.

๐Ÿ“ถ 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 in server.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 to http://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

url-shorten-microservice's People

Contributors

andrewjbateman avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.