Add URL shortener certification project

This commit is contained in:
CherryKitten 2022-10-24 19:28:16 +02:00
parent 82bbfefc5e
commit 1097d1dc85
Signed by: sammy
GPG key ID: 0B696A86A853E955
12 changed files with 1880 additions and 0 deletions

View file

@ -0,0 +1,2 @@
# Tell Linguist to exclude HTML files to help Replit language detection.
*.html linguist-vendored

View file

@ -0,0 +1,3 @@
.vscode/*
.env
node_modules/

View file

@ -0,0 +1 @@
.replit

View file

@ -0,0 +1,10 @@
run = "npm start"
entrypoint = "index.js"
[packager]
language = "nodejs"
[packager.features]
packageSearch = true
guessImports = true
enabledForHosting = false

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

View 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}`);
});

View 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"
}

File diff suppressed because it is too large Load diff

View 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"
}
}

View 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;
}

View file

@ -0,0 +1 @@
PORT=

View 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>