Long URL's are made short using Node.js, Express.js along with MongoDB. Just have to paste the URL and short link will be created.
Make sure you have VS-Code installed along with Node. In the terminal, do npm init. This is going to initialize the folder for us which can do all the default values entered a bunch of times. Then, install all the necessary packages - npm i express mongoose ejs. This allows us to create our views, our templating language so that we can make our HTML and our backend work together and send useful data down to the browser. We need Dev Dependencies - npm i --save-dev nodemon. Whenever we make some changes in our file, nodemon will automatically refresh the server.
Our file App.js will contain all the posts and the get request made throughout. We require express and set up our port 3000. Our file will just open in the localhost::3000. Each route will contain a function with request and response variables and render our ejs file. We use .ejs as it is the templating language that will be used. We are rendering everything in our ejs to our browser.
We create a simple UI by making a form using HTML and Bootstrap.
Create a model which will store all our short URL information. We require a mongoose. We create a new schema that takes just an object which is going to be indifferent columns in our database. Inside we have a full URL, a short URL, and a number of times we have clicked that link(basically count).
All the get and post routes are added and functionality is checked.
We just have to paste the long URL in the box, a short URL will be generated along with the count and we will be redirected to the required page using the short URL created.
If case you want to deploy this site using git or GitHub and don't want to send all the modules up to the server, you just want to send package.json. So, with .gitignore we ignore all our node modules.
Submitted by Anushka Priya (Anushkaa)
Download packets of source code on Coders Packet