Skip to content

A simple local running URL Shortener with Node.js and MongoDB

Notifications You must be signed in to change notification settings

palmavilluan/nodeShortener

 
 

Repository files navigation

nodeShortener

A simple local running URL Shortener with Node.js and MongoDB.

Preparation

  1. Install MongoDB
  2. Start a new Project with npm init
  3. Install the required packages
  4. Get Postman Login
  5. Get MongoDB Compass
  6. Install nodemon globally with npm install -g nodemon
  7. Add in the package.json: "type": "module"

Requirements

  • Node.js
  • MongoDB
  • Postman
  • mongoDB Compass

NPM Packages

  • Express
  • Mongoose
  • nanoid
  • nodemon (global)
  • pug

Start

Add this folders

  • config
  • data
  • middleware
  • models
  • routes
  • public
  • views Add this file
  • index.js

First Steps with Express

  • 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.

01_start

After this steps your Project should look like the folder "01_start".

Routes

  • 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().

02_routes

In the folder "02_routes" you can find the finished routes.

Models

  • 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.

03_models

In the folder "03_models" you can find the finished models.

Validation

  • 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.

04_validation

In the folder "04_validation" you can find the finished validation.

Redirection

  • 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.

05_redirection

In the folder "05_redirection" you can find the finished redirection.

Frontend

  • 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.

Final Project

In the folder "06_final" you can find the finished project.

About

A simple local running URL Shortener with Node.js and MongoDB

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 82.5%
  • CSS 13.8%
  • Pug 3.7%