![]() ![]() To make our routes work, we have to use a middleware pattern such as Express. post( '/shorten', async ( req, res) => `)) To add this file, create a separate file named redirect.js inside the routes folder.Īdd the following code inside the url.js file:Ĭonst baseUrl = 'http:localhost:5000' router. It takes the short URL and redirects it to the actual long URL in the browser. Redirect.js: This is a GET for our URL redirects. Navigate in the routes folder and create a file named url.js. Url.js: This will be a POST route that takes an incoming request with the long URL and creates the short URL and inserts it into the database. Inside the URL-Shortener-Service folder, create a folder named routes. The date property has a default property and is created once the model is instantiated in the database. The property shortUrl is the actual short URL that will be generated. The longUrl is the default URL which we need to shorten. The urlCode is a string property that will store the unique ID related to each URL. This object takes the values that our MongoDB document will store. The mongoose.Schema method is instantiated to define with and object argument. To create a model, we need to create a schema interface by importing the mongoose npm package. create a model from schema and export it Inside this folder, add a file named UrlModel.js and add the following code: To create our model, create a folder named models. The model is what we use to find, create, update, and delete documents of a given type. Schemas will then be transformed into models using the mongoose.model() method. A Schema will allow us to define all the fields stored in each document along with the validation or default values. When using mongoose, models are defined using a Schema interface. module.exports exports the connection that will be added in our index.js server entry file. The nnect() is a method that takes the DB_URI and an options object to establish a connection. ![]() This is named as the DB_URI connection string with the urlshortener as the database name. To start a connection on our MongoDB database, we need a database connection port. The const mongoose = require('mongoose') imports the mongoose package from the node_modules folder. connection // export the connection object Run the following command on your terminal to download them:Ĭonst DB_URI = 'mongodb://localhost:27017/urlshortener' // establishing a database connectionĬonst connection = mongoose. Next, we now need to download the packages. It will constantly monitor our applications by automatically restarting the server when any file changes. Nodemon: nodemon package will be installed as a development dependency. Valid-url: This is a module that verifies all the URLs sent to the API. Short-id: The short-id module creates user-friendly and unique ids for our URLs. It is responsible for connecting to the database and performing query operations. Mongoose: Mongoose is an asynchronous database driver or Object Data Mapper for MongoDB. Our project will use various npm packages from the npm registry.Įxpress: Express is a backend web application framework for Node.js used for building web applications and APIs. This command will require some prompts, to skip this, you can use npm init -y. Go inside the folder and type npm init to generate an initial package.json for our project. MongoDB database server installed on your system.Ĭreate a folder named URL-Shortener-Service and open it on your favorite IDE. Have Postman HTTP API client installed on your system. PrerequisitesĪ basic understanding of the JavaScript programming language.Ī basic understanding of Node.js and Express framework. In this tutorial, we will build a URL shortener service using Node.js. The benefit of URL shortening tools is in helping drive traffic back to your website. ![]() Doing this will help you promote your products or any services with the links provided. Long URL links are not the best way to post these links, especially on social media. Sometimes we need to advertise our businesses on social media. Using links or URLs has been the norm for surfing the web for a long time. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |