Added ability to deploy on Heroku

This commit is contained in:
Sunny Dhoke
2020-10-09 16:59:37 +05:30
parent a2acc098a7
commit 5453470e43
3 changed files with 19 additions and 2 deletions

View File

@@ -33,3 +33,16 @@ make sure admin access
- to start server
"C:\Program Files\MongoDB\Server\4.4\bin\mongo.exe"
## Deploy
### Heroku
1. Fork this repository
1. Create a free Heroku Account
1. Create free heroku app with your github fork
## Roadmap
- A login Screen
- protected routes with JWT

View File

@@ -4,6 +4,7 @@
"description": "A url shortner",
"main": "index.js",
"scripts": {
"start": "node ./server.js",
"devStart": "nodemon server.js"
},
"repository": {

View File

@@ -12,7 +12,9 @@ app.use(express.urlencoded({ extended: false }));
// to connect to database
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/db", {
//App deploy or localhost mode connection to MongoDB
mongoose.connect(process.env.MONGO_URL || "mongodb://localhost/db", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
@@ -46,7 +48,8 @@ app.get("/:shortUrl", async (req, res) => {
//otherwise increase the click
shortUrl.clicks++;
//save the changes in table
shortUrl.save();
//can use `await` here but speed of redirect is more important
await shortUrl.save();
//redirect the full link corresponding the short url we found
res.redirect(shortUrl.full);