From eeb195793c9985c7dd04cc6a09cc82d48740df85 Mon Sep 17 00:00:00 2001 From: Sunny Dhoke Date: Thu, 8 Oct 2020 13:52:48 +0530 Subject: [PATCH] Working prototype. --- README.md | 12 ++++++++++++ Views/index.ejs | 15 +++++++++------ models/shortUrl.js | 6 +++--- server.js | 23 ++++++++++++++++++----- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index c31c5bb..1f38071 100644 --- a/README.md +++ b/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" diff --git a/Views/index.ejs b/Views/index.ejs index 2410678..99c1e32 100644 --- a/Views/index.ejs +++ b/Views/index.ejs @@ -15,13 +15,13 @@

URL Shortner

- + @@ -39,13 +39,16 @@ + <% shortUrls.forEach(shortUrl => { %> + - https://sunn-e.github.io + <%= shortUrl.full %> - 420 - 1 + <%= shortUrl.short %> + <%= shortUrl.clicks %> + <% }) %> });
diff --git a/models/shortUrl.js b/models/shortUrl.js index 359e33c..b519976 100644 --- a/models/shortUrl.js +++ b/models/shortUrl.js @@ -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); diff --git a/server.js b/server.js index f5c72fe..c72c6fc 100644 --- a/server.js +++ b/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);