FreeCodeCamp-APIs and Microservices — URL Shortener Microservice

FreeCodeCamp
  • Create a MongoDB database on MongoDB Atlas.
  • Install all the required npm packages.
  • Define the environment variables.
  • Create an express server.
  • Connect to the database.
  • Create the URL Model.
  • Create the API routes post and get methods.
  • Conclusion
MongoDB — Connection String
  • Express : A node.js framework that makes it easy to build web
    applications.
  • Mongodb : Official MongoDB driver for Node.js
  • Mongoose : An object modeling tool designed to work in an
    asynchronous environment. We will use mongoose to define database
    schemas and interact with the database.
  • Cors : CORS is a node.js package for providing a Connect/Express middleware that can be use to enable CORS with various options.
  • Body-parser :Node.js body parsing middleware. Parse incoming request bodies in a middleware before your handlers, available under the req.body property.
  • Valid-url : This module collects common URI validation routines to make input validation, and untainting easier and more readable.
  • Shortid : Amazingly short non-sequential url-friendly unique id generator.
package.json
MONGO_URI=’mongodb+srv://<yourusername>:<yourpassword>@cluster0-lewgd.mongodb.net/test?retryWrites=true&w=majority’
Node.js listening ...
  • HTTP POST /api/shorturl/new — Create new url
  • HTTP GET /api/shorturl/:short_url? — Get original url via shorturl

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store