The aim of this project was to build a small web app with functionality similar to: https://url-shortener-microservice.freecodecamp.rocks
The project was built using the following technologies:
- HTML
- JavaScript with Node.js / NPM for package management.
- Express web framework to build the web API.
- mongoose for MongoDB object modeling, interacting with a MongoDB Atlas database.
- Bootstrap for styling with some custom CSS.
- FontAwesome for icons.
- nodemon for automatic restarting of server during development.
-
User Story #1: You can POST a URL to
/api/shorturl
and get a JSON response withoriginal_url
andshort_url
properties. Here's an example:{ original_url : 'https://freeCodeCamp.org', short_url : 1}
-
User Story #2: When you visit
/api/shorturl/<short_url>
, you will be redirected to the original URL. -
User Story #3: If you pass an invalid URL that doesn't follow the valid
http://www.example.com
format, the JSON response will contain{ error: 'invalid url' }
The third Free Code Camp: Back End Development Project is a URL shortener microservice. Users can:
-
Submit a URL to be shortened using the form on the app home page, or by sending a POST request to
/api/shorturl
with a urlencoded body containing the field 'url' containing a valid URL. -
Be redirected to the original URL by sending a GET request to
/api/shorturl/<SHORT_URL>
whereSHORT_URL
is theshort_url
string returned via JSON when a URL to be shortened is submitted.
Shortened URL records are stored in a MongoDB database, and expire after 24hrs.
-
index.js
- the main entry point of the application, an express web server handling the routes defined in the specification. -
public/
- contains static files for the web app (stylesheet, logo, favicons etc), served by express usingexpress.static()
. -
views/
- contains the single html page for the web app,index.html
, which is served by express onGET
requests to/
. -
middleware/
- contains express middleware functions used by the two major routes of the express server. -
models/
- containsmongoose
schema and models for the database interactions of the app. -
helpers/
- contains two small helper files used by some of the middleware functions.
Requires Node.js / NPM in order to install required packages. After downloading the repo, install required dependencies with:
npm install
To run the app locally, a valid MongoDB database URI is required to be entered as an environmental variable (MONGO_URI), which can be done via a .env
file (see sample.env). One possible MongoDB service is MongoDB Atlas.
A development mode (with auto server restart on file save), can be started with:
npm run dev
The application can then be viewed at http://localhost:3000/
in the browser.
To start the server without auto-restart on file save:
npm start
The initial boilerplate for this app can be found at https://url-shortener-microservice.freecodecamp.rocks/
Instructions for building the project can be found at https://www.freecodecamp.org/learn/back-end-development-and-apis/back-end-development-and-apis-projects/url-shortener-microservice