mirror of
https://github.com/SrIzan10/YouRL.git
synced 2026-05-01 11:15:09 +00:00
Working prototype.
This commit is contained in:
12
README.md
12
README.md
@@ -21,3 +21,15 @@ ejs- templating language to create views
|
||||
`npm i --save-dev nodemon`
|
||||
//npm install -g nodemon
|
||||
For refreshing server for every new change
|
||||
|
||||
### mongodb on windows
|
||||
|
||||
make sure admin access
|
||||
|
||||
- to access db
|
||||
|
||||
"C:\Program Files\MongoDB\Server\4.4\bin\mongod.exe" --dbpath="c:\data\db"
|
||||
|
||||
- to start server
|
||||
|
||||
"C:\Program Files\MongoDB\Server\4.4\bin\mongo.exe"
|
||||
|
||||
@@ -15,13 +15,13 @@
|
||||
<div class="container">
|
||||
<h1>URL Shortner</h1>
|
||||
<form action="shortUrls" method="POST" class="my-4 form-inline">
|
||||
<label for="fullurl" class="sr-only">URL</label>
|
||||
<label for="fullUrl" class="sr-only">URL</label>
|
||||
<input
|
||||
required
|
||||
placeholder="Enter URL"
|
||||
type="url"
|
||||
name="fullurl"
|
||||
id="fullurl"
|
||||
name="fullUrl"
|
||||
id="fullUrl"
|
||||
class="form-control mr-1 col"
|
||||
/>
|
||||
<button type="submit" class="btn btn-success">Shorten my link!</button>
|
||||
@@ -39,13 +39,16 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% shortUrls.forEach(shortUrl => { %>
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
<a href="https://sunn-e.github.io">https://sunn-e.github.io</a>
|
||||
<a href="<%= shortUrl.full %>"><%= shortUrl.full %></a>
|
||||
</td>
|
||||
<td><a href="/420">420</a></td>
|
||||
<td>1</td>
|
||||
<td><a href="<%= shortUrl.short %>"><%= shortUrl.short %> </a></td>
|
||||
<td><%= shortUrl.clicks %></td>
|
||||
</tr>
|
||||
<% }) %> });
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
@@ -8,11 +8,11 @@ const shortId = require("shortid");
|
||||
//db schema
|
||||
// | fullurl | shorturl | clicks |
|
||||
const shortUrlSchema = new mongoose.Schema({
|
||||
fullurl: {
|
||||
full: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
shorturl: {
|
||||
short: {
|
||||
type: String,
|
||||
required: true,
|
||||
default: shortId.generate,
|
||||
@@ -26,4 +26,4 @@ const shortUrlSchema = new mongoose.Schema({
|
||||
|
||||
//now exporting this to hook model with database
|
||||
// model name, schema name
|
||||
modules.exports = mongoose.model("shortUrl", shortUrlSchema);
|
||||
module.exports = mongoose.model("shortUrl", shortUrlSchema);
|
||||
|
||||
23
server.js
23
server.js
@@ -7,19 +7,32 @@ const app = express();
|
||||
//setting ejs as view engine
|
||||
app.set("view engine", "ejs");
|
||||
|
||||
// tell express, app is using url parameters
|
||||
app.use(express.urlencoded({ extended: false }));
|
||||
|
||||
// to connect to database
|
||||
const mongoose = require("mongoose");
|
||||
mongoose.connect("mongodb://localhost/urlShortner", {
|
||||
mongoose.connect("mongodb://localhost/db", {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
});
|
||||
|
||||
// import data model/ schema
|
||||
const ShortUrl = require("./models/shortUrl");
|
||||
|
||||
//get
|
||||
// index page with request and response parameters
|
||||
app.get("/", (req, res) => {
|
||||
res.render("index");
|
||||
// show all url in index page
|
||||
app.get("/", async (req, res) => {
|
||||
const shortUrls = await ShortUrl.find();
|
||||
res.render("index", { shortUrls: shortUrls });
|
||||
});
|
||||
|
||||
//post
|
||||
app.post("/shortUrls", (req, res) => {});
|
||||
// start listening on specified port
|
||||
app.post("/shortUrls", async (req, res) => {
|
||||
ShortUrl.create({ full: req.body.fullUrl });
|
||||
res.redirect("/");
|
||||
});
|
||||
// start listening on specified port 4567
|
||||
//can set as environment var
|
||||
app.listen(process.env.PORT || 4567);
|
||||
|
||||
Reference in New Issue
Block a user