-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
30 lines (27 loc) · 1021 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const express = require("express");
const app = express();
require('dotenv').config();
const PORT =process.env.PORT||8001;
const path = require("path");
const Url=require('./models/url');
const urlRoutes=require('./routes/url');
const {connectMongoDB}=require('./connect');
const staticRoute = require('./routes/staticRouter');
connectMongoDB(process.env.MONGO_URL).then(() => console.log("MONGODB CONNECTED"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.set("view engine","ejs");
app.set("views",path.resolve("./views"));
app.use("/",staticRoute);
app.use("/url",urlRoutes);
app.get("/url/:shortId",async (req,res)=>{
const shortId=req.params.shortId;
const data=await Url.findOneAndUpdate({shortId:shortId},{$push:{clickHistory:{timestamp:Date.now()}}});
if(!data){
return res.status(404).json({error:'url not found'});
}
else{
res.redirect(data.redirectUrl);
}
});
app.listen(PORT,()=>console.log('server started at port 3000'));