A simple local running URL Shortener with Node.js and MongoDB.
- Install MongoDB
- Start a new Project with
npm init
- Install the required packages
- Get Postman Login
- Get MongoDB Compass
- Install nodemon globally with
npm install -g nodemon
- Add in the package.json: "type": "module"
- Node.js
- MongoDB
- Postman
- mongoDB Compass
- Express
- Mongoose
- nanoid
- nodemon (global)
- pug
Add this folders
- config
- data
- middleware
- models
- routes
- public
- views Add this file
- index.js
- We start with the index.js file.
- We need to import express and create an express app.
- Then we need to create a server with the app.listen() method.
- After this we can start the server with nodemon.
- After that we can see a result at localhost:3000.
- We start the database connection with mongoose.connect().
- Then we can see a result in the console.
After this steps your Project should look like the folder "01_start".
- We add a urls.js file in the routes folder.
- In this file we create a router with express.Router().
- Then we can add the routes with router.get().
- After this we need to export the router with module.exports.
- In the index.js file we need to import the router with require().
- Then we can use the router with app.use().
In the folder "02_routes" you can find the finished routes.
- We add a url.js file in the models folder.
- In this file we create a schema with mongoose.Schema().
- Then we can create a model with mongoose.model().
- After this we need to export the model with module.exports.
- We add the model into the routes.
- Then we can use the model with app.use().
- Now we can test the routes with Postman.
In the folder "03_models" you can find the finished models.
- We add JOI to the models.
- Then we can add the validation to the model.
- After this we can add the validation to the routes.
- Now we can test the validation with Postman.
In the folder "04_validation" you can find the finished validation.
- We add a redirect route to the routes with :shortUrl.
- Then we can add the redirect to the routes.
- Now we can test the redirect.
In the folder "05_redirection" you can find the finished redirection.
- We add a views folder with a index.pug file.
- We add css in the public folder.
- Then we can add the frontend to the routes.
- Now we can test the frontend.
In the folder "06_final" you can find the finished project.