Add URL shortener certification project
This commit is contained in:
parent
82bbfefc5e
commit
1097d1dc85
12 changed files with 1880 additions and 0 deletions
2
6-backend-dev/3-url-shortener/.gitattributes
vendored
Normal file
2
6-backend-dev/3-url-shortener/.gitattributes
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
# Tell Linguist to exclude HTML files to help Replit language detection.
|
||||||
|
*.html linguist-vendored
|
3
6-backend-dev/3-url-shortener/.gitignore
vendored
Normal file
3
6-backend-dev/3-url-shortener/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
.vscode/*
|
||||||
|
.env
|
||||||
|
node_modules/
|
1
6-backend-dev/3-url-shortener/.prettierignore
Normal file
1
6-backend-dev/3-url-shortener/.prettierignore
Normal file
|
@ -0,0 +1 @@
|
||||||
|
.replit
|
10
6-backend-dev/3-url-shortener/.replit
Normal file
10
6-backend-dev/3-url-shortener/.replit
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
run = "npm start"
|
||||||
|
entrypoint = "index.js"
|
||||||
|
|
||||||
|
[packager]
|
||||||
|
language = "nodejs"
|
||||||
|
|
||||||
|
[packager.features]
|
||||||
|
packageSearch = true
|
||||||
|
guessImports = true
|
||||||
|
enabledForHosting = false
|
3
6-backend-dev/3-url-shortener/README.md
Normal file
3
6-backend-dev/3-url-shortener/README.md
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# URL Shortener Microservice
|
||||||
|
|
||||||
|
This is the boilerplate code for the URL Shortener Microservice project. Instructions for building your project can be found at https://www.freecodecamp.org/learn/back-end-development-and-apis/back-end-development-and-apis-projects/url-shortener-microservice.
|
40
6-backend-dev/3-url-shortener/index.js
Normal file
40
6-backend-dev/3-url-shortener/index.js
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
require('dotenv').config();
|
||||||
|
const express = require('express');
|
||||||
|
const cors = require('cors');
|
||||||
|
const dns = require("dns");
|
||||||
|
const app = express();
|
||||||
|
const shorturls = ['https://freecodecamp.org']
|
||||||
|
|
||||||
|
// Basic Configuration
|
||||||
|
const port = process.env.PORT || 3000;
|
||||||
|
|
||||||
|
app.use(cors());
|
||||||
|
|
||||||
|
app.use('/public', express.static(`${process.cwd()}/public`));
|
||||||
|
app.use(express.json()) // for parsing application/json
|
||||||
|
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
|
||||||
|
|
||||||
|
app.get('/', function(req, res) {
|
||||||
|
res.sendFile(process.cwd() + '/views/index.html');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Your first API endpoint
|
||||||
|
|
||||||
|
app.get('/api/shorturl/:id', (req, res) => {
|
||||||
|
res.redirect(shorturls[req.params.id])
|
||||||
|
})
|
||||||
|
|
||||||
|
app.post('/api/shorturl', (req, res) => {
|
||||||
|
const url = req.body.url;
|
||||||
|
const id = shorturls.length;
|
||||||
|
const domain = (new URL(url)).host
|
||||||
|
dns.lookup(domain, (err) => {
|
||||||
|
if (err) return res.json({error: 'invalid url'})
|
||||||
|
shorturls.push(url);
|
||||||
|
res.json({ original_url : url, short_url : id})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
app.listen(port, function() {
|
||||||
|
console.log(`Listening on port ${port}`);
|
||||||
|
});
|
13
6-backend-dev/3-url-shortener/nodemon.json
Normal file
13
6-backend-dev/3-url-shortener/nodemon.json
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"restartable": "rs",
|
||||||
|
"ignore": [
|
||||||
|
".git",
|
||||||
|
"node_modules/**/node_modules"
|
||||||
|
],
|
||||||
|
"verbose": true,
|
||||||
|
"env": {
|
||||||
|
"NODE_ENV": "development"
|
||||||
|
},
|
||||||
|
"ext": "js,json,html,css",
|
||||||
|
"delay": "2500"
|
||||||
|
}
|
1709
6-backend-dev/3-url-shortener/package-lock.json
generated
Normal file
1709
6-backend-dev/3-url-shortener/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
20
6-backend-dev/3-url-shortener/package.json
Normal file
20
6-backend-dev/3-url-shortener/package.json
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
{
|
||||||
|
"name": "shorturl",
|
||||||
|
"version": "0.0.3",
|
||||||
|
"description": "API project for freeCodeCamp",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node index.js",
|
||||||
|
"dev": "nodemon index.js"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"body-parser": "^1.19.0",
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"dotenv": "^8.2.0",
|
||||||
|
"express": "^4.17.1"
|
||||||
|
},
|
||||||
|
"license": "MIT",
|
||||||
|
"devDependencies": {
|
||||||
|
"nodemon": "^2.0.4"
|
||||||
|
}
|
||||||
|
}
|
46
6-backend-dev/3-url-shortener/public/style.css
Normal file
46
6-backend-dev/3-url-shortener/public/style.css
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
font-size: 16px;
|
||||||
|
color: #222;
|
||||||
|
background-color: #fff;
|
||||||
|
text-align: center;
|
||||||
|
line-height: 1.4em;
|
||||||
|
}
|
||||||
|
|
||||||
|
main {
|
||||||
|
padding: 0;
|
||||||
|
margin-top: 40px;
|
||||||
|
}
|
||||||
|
|
||||||
|
h3 {
|
||||||
|
margin-top: 30px;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
.user-stories li {
|
||||||
|
margin-bottom: 1em;
|
||||||
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
color: #2574a9;
|
||||||
|
}
|
||||||
|
|
||||||
|
form {
|
||||||
|
margin: 10px auto;
|
||||||
|
padding: 20px;
|
||||||
|
max-width: 600px;
|
||||||
|
}
|
||||||
|
|
||||||
|
label {
|
||||||
|
margin-right: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
input {
|
||||||
|
padding: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
input[type='text'] {
|
||||||
|
width: 220px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
1
6-backend-dev/3-url-shortener/sample.env
Normal file
1
6-backend-dev/3-url-shortener/sample.env
Normal file
|
@ -0,0 +1 @@
|
||||||
|
PORT=
|
32
6-backend-dev/3-url-shortener/views/index.html
vendored
Normal file
32
6-backend-dev/3-url-shortener/views/index.html
vendored
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>URL Shortener Microservice | freeCodeCamp.org</title>
|
||||||
|
<link
|
||||||
|
rel="icon"
|
||||||
|
type="image/png"
|
||||||
|
href="https://cdn.freecodecamp.org/universal/favicons/favicon-16x16.png"
|
||||||
|
/>
|
||||||
|
<link href="/public/style.css" rel="stylesheet" type="text/css" />
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<h1>URL Shortener Microservice</h1>
|
||||||
|
<main>
|
||||||
|
<section>
|
||||||
|
<form action="api/shorturl" method="POST">
|
||||||
|
<fieldset>
|
||||||
|
<legend>URL Shortener</legend>
|
||||||
|
<label for="url_input">URL:</label>
|
||||||
|
<input id="url_input" type="text" name="url" placeholder="https://www.freecodecamp.org/" />
|
||||||
|
<input type="submit" value="POST URL" />
|
||||||
|
</fieldset>
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
</main>
|
||||||
|
<footer>
|
||||||
|
<p>By <a href="https://github.com/CherryKitten">CherryKitten</a></p>
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
|
</html>
|
Reference in a new issue