Skip to content

Commit

Permalink
Merge pull request #4 from fawazsullia/fawaz-backend
Browse files Browse the repository at this point in the history
Fawaz backend
  • Loading branch information
tushar821999 authored Jun 9, 2021
2 parents 903a7e0 + 395fdb9 commit 24c995d
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.env
.env
apitest.http
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<<<<<<< HEAD
# Link-Trim
A minimal web app for URL shortner service

# Branches
1. **Main** : <a href="https://github.com/WE-FOSS/Link-Trim/tree/main">Checkout</a>
2. **Web-Services-API** : <a href="https://github.com/WE-FOSS/Link-Trim/tree/Web-Services-API">Checkout</a>
=======
# Link-Trim Web Service API 🚀

<img src="https://cdn.discordapp.com/attachments/848998092518064153/849281320525758494/Logo---WeFoss.jpg" height=250px width=250px>
Expand Down Expand Up @@ -65,3 +73,4 @@
- Varun
- Bilalumrani
- Fawaz
>>>>>>> Web-Services-Api
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "link-trim",
"version": "1.0.0",
"description": "A minimal web app for URL shortner service",
"main": "index.js",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node src/app.js",
Expand All @@ -23,6 +23,7 @@
"dotenv": "^10.0.0",
"express": "^4.17.1",
"mongoose": "^5.12.12",
"nanoid": "^3.1.23",
"nodemon": "^2.0.7",
"validator": "^13.6.0"
}
Expand Down
51 changes: 9 additions & 42 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express = require('express');

const app = express();
const DNS = require('./models/urls')
const allRoutes = require('./routes/allRoutes.js')

// require db connection
require('./db/connection');
Expand All @@ -10,47 +11,13 @@ const port = process.env.PORT || 3000;

app.use(express.json())

app.get("/:inpURL",async(req,res)=>{
try{
const getUrl = await DNS.find({
"inputText": req.params.inpURL
});
res.status(201).send(getUrl);
}catch(e){
res.status(400).send(e);
}
});

app.get("/",async(req,res)=>{
try{
const getUrl = await DNS.find({

});
res.status(201).send(getUrl);
}catch(e){
res.status(400).send(e);
}
});

// Create a new URL
app.post("/url",(req,res)=>{
const inputURL = req.body.inputURL;
const inputText = req.body.inputText;
const outputText = req.get('host')+"/"+inputText;

const body = {
"inputURL" : inputURL,
"inputText" : inputText,
"outputText" : outputText
}
console.log(body)
const newDNS = new DNS(body)
newDNS.save().then(()=>{
res.status(201).send(newDNS);
}).catch((e)=>{
res.status(400).send(e);
})
});

//Routes middleware
app.use('/', allRoutes)






app.listen(port,()=>{
Expand Down
4 changes: 4 additions & 0 deletions src/db/connection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require('dotenv').config()
const mongoose = require('mongoose');


const connectionURL = "mongodb+srv://"+process.env.DB_USER_NAME+":"+process.env.DB_PASSWORD+"@we-foss.qpkiw.mongodb.net/"+process.env.DB_DATABASE_NAME+"?retryWrites=true&w=majority/DNS-map";


mongoose.connect(connectionURL,{
useCreateIndex:true,
useNewUrlParser:true,
Expand Down
66 changes: 66 additions & 0 deletions src/routes/allRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const express = require('express');
const router = express.Router();
const {nanoid} = require('nanoid')
const DNS = require('../models/urls')




router.get("/:inpURL",async(req,res)=>{

try{
const { inputURL } = await DNS.findOne({
"inputText": req.params.inpURL
}, 'inputURL');


await res.redirect(302,inputURL);
}catch(e){
res.status(400).send(e).end();
}
});



router.get("/",async(req,res)=>{
try{
const getUrl = await DNS.find({

});
res.status(201).send(getUrl);
}catch(e){
res.status(400).send(e);
}
});



// Create a new URL
router.post("/url", async (req,res)=>{

try{
const randomstring = nanoid(4);
const inputURL = req.body.inputURL;
const inputText = `${randomstring}-${req.body.inputText}`;
const outputText = inputText!== "" ? req.get('host')+"/"+inputText : req.get('host')+"/"+nanoid(10);


const body = {
"inputURL" : inputURL,
"inputText" : inputText,
"outputText" : outputText
}

const newDNS = new DNS(body);

await newDNS.save();
await res.status(201).send(newDNS);
}

catch(e){
res.status(400).send(e);

}}
)

module.exports = router

0 comments on commit 24c995d

Please sign in to comment.