Demos
We'll cover the making of this whole repository in small demos If you didn't cover the basic EJS project repo, then visit this link
1-Setting-up-the-Backend
On the Command line navigate to the directory where you want to save the project on your system, then type in the following commands
mkdir URLShortener
cd URLShortener
npm init -y
- What this will do is that it will keep init output as per the default template.
- Explore what
package.json
is. Mainly mention the entrypoint which is main: index.js
- How to open the terminal in VSCode (
View > Terminal
)
- Let's install other libraries as well at once, so type in the below command:
npm install express ejs config mongoose shortid valid-url --save
-
- What the above step will do is it will record the names of the dependencies which are required to run the package.
- Create the file
index.js
- Type in/snippet/copy/paste the following content.
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
// ..
app.get('/', (req, res) => res.send('Hello world'));
app.listen(port, () => console.log('Application started at => http://localhost:'+port));
- Run the code by this command:
node index.js
and go to localhost:3000. To see the Home Page.
- Next, let's just get rid of this continuously pressing F5 or typing in
node index.js
again and again.
- So to automate this process we have a package called "nodemon". So let's install it:
npm i nodemon --save-dev
- Now let's see the
package.json
file, and add some script to it, so that it can trigger nodemon whenever we run that dev script.
- Replace the below snippet with the later one:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
}
"scripts": {
"start": "node index",
"dev": "nodemon index"
}
- Use the below code snippet in the
index.js
file (remove all the previous code):
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json({
extended: false
}));
app.use(express.urlencoded({
extended: false
}));
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('home');
});
app.listen(port, () => console.log('Application started at => http://localhost:' + port));
- Here the statement app.use(express.json(-------)); is a Middleware that will allow us to accept JSON data into our API.
Setting-up-Directory-structure
- Create folders & files inside the project directory as per the below-given List:
- config (Folder)
- models (Folder)
- routes (Folder)
- views (Folder)
Building-a-basic-Front-End
- Now use the below snippet for the
home.ejs
file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>UNITE- URL Shortner 😍
</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
<div class="container-fluid">
<div class="jumbotron" style="text-align: center;">
<h1 class="display-4">Welcome to the URL Shortner
</h1>
</div>
<!-- FORM -->
<!-- URLs Table -->
<footer class="footer container-fluid my-4">
<hr>
<span class="row justify-content-center text-muted"> Copyright
<% const date = new Date(); %>
<%= date.getFullYear() %>
</span>
</footer>
</div>
</body>
</html>
- In this above code we've used a 3rd party frontend CSS library to speed up our front end jobs beautifully.
- Now let's see the implementation of the said tool
nodemon
.
- in the command line, preferably in the VS Code, type:
npm run dev
- Now comes the snippet for the URL shortening form in the
home.ejs
file. Place the below snippet right below the <!-- FORM -->
Comment in the HTML file:
<!-- FORM -->
<form action="/shorten/" method="POST" style="padding: 0 25px 0 25px;">
<div class="form-group row justify-content-md-center">
<label class="col-lg-1 col-md-10" for="longUrl">Long URL:
</label>
<input required="" type="url" class="col-lg-3 col-12 form-control" name="longUrl" id="longUrl" placeholder="Enter your Name">
</div>
<div class="form-group row justify-content-md-center">
<label class="col-lg-1 col-md-10" for="shortUrl">Short URL:
</label>
<input type="text" class="col-lg-3 col-12 form-control" id="shortUrl" name="shortUrl" placeholder="Enter the custom name (if any)">
</div>
<div class="form-group row justify-content-md-center">
<button type="submit" class="col-12 col-lg-4 btn btn-primary">Submit
</button>
</div>
</form>
and the Table code below the Table Comment <!-- URLs Table -->
:
<!-- URLs Table -->
<div style="margin: auto;" class="row justify-content-md-center table-responsive-sm">
<table style="padding: 0 25px 0 25px;" class="table table-hover col-12 col-md-10 col-lg-6">
<thead>
<tr>
<th># ID
</th>
<th>Long URL
</th>
<th>Short URL
</th>
<th>Clicks
</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">#
</th>
<td>
<a target="_blank" href=#">Long URL Comes Here</a></td> <td><a target="_blank" href="#">Short URL Comes Here</a></td> <td>0</td> </tr> </tbody> </table> </div>
- Now refresh the browser page