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 @@
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);