-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
142 lines (119 loc) · 4.64 KB
/
server.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import express from 'express'
import { MongoClient, MongoServerError } from 'mongodb'
import ViteExpress from "vite-express";
// Definitions
const { MONGO_ADMIN_USER, MONGO_ADMIN_PASS, ENV_LOCAL } = process.env // Import user and pass from SYSTEM environments
// add LINUX variables by editing "nano ~/.profile" and adding "export MONGO_ADMIN_USER=username"
// add WINDOWS variables with "setx ENV_LOCAL true"
// add JENKINS variables with "credentials"
// OR in docker compose environment
// Database endpoint check and connect
let mongo = undefined
if (ENV_LOCAL) { // Local environment
mongo = new MongoClient(`mongodb://127.0.0.1:27017`)
} else { // use container name when starting application as docker container, part of docker-compose
mongo = new MongoClient(`mongodb://${MONGO_ADMIN_USER}:${MONGO_ADMIN_PASS}@ub-mongodb`)
}
const db = mongo.db('utm-builder')
// Start express server
const app = express() // Initialize the express server
// Middleware
app.use(express.json()) // needed to parse the JSON to JS first, otherwise you gat an error!
//app.use(express.static('dist')) // serves the index.html file on load from the dist folder, so you can use the frontend app on the express app port (e.g. - localhost:3000). This is actually not needed if you configure the vite.config server.proxy!
// GET Tagged URLs endpoint
app.get('/api/users/:clientId/get-tagged-urls', async (req, res) => {
const clientId = req.params.clientId
// Connect to DB
await mongo.connect()
console.log('Connected to DB successfully')
try {
// get data from db
const getUrls = await db.collection('taggedUrls')
.find({ clientId: clientId })
.sort({createdAt: -1}) // Sort by _id in descending order to get the latest items first
.limit(15) // Limit the results to the last 10 items
.project({ taggedUrl: 1 })
.toArray()
// Send a response to frontend
res.json(getUrls)
} catch (error) {
console.error(error)
} finally {
// Disconnect
await mongo.close()
console.log('Disconnected from DB successfully')
}
})
// POST Tagged URLs endpoint (RequestHandler function to handle requests and responses (req, res))
app.post('/api/users/:clientId/save-tagged-url', async (req, res) => {
const payload = req.body
// Connect to DB
await mongo.connect()
console.log('Connected to DB successfully')
// Save payload to DB with expiration time
try {
payload['createdAt'] = new Date()
await db.collection('taggedUrls').insertOne(payload)
await db.collection('taggedUrls').createIndex({createdAt: 1}, { expireAfterSeconds: 86400 })
// Send a response to frontend
res.json(payload)
} catch(error) {
if (error instanceof MongoServerError) {
console.error(`There is an error: ${error}`)
}
throw error
} finally {
// Disconnect
await mongo.close()
console.log('Disconnected from DB successfully')
}
})
// POST Definitions endpoint
app.post('/api/users/:clientId/save-definitions', async (req, res) => {
const payload = req.body
// Connect to DB
await mongo.connect()
console.log('Connected to DB successfully')
// Save payload to DB with expiration time
try {
payload['createdAt'] = new Date()
await db.collection('definitions').updateOne({ _id: payload._id }, { $set: payload }, { upsert: true })
await db.collection('definitions').createIndex({createdAt: 1}, { expireAfterSeconds: 86400 })
// Send a response to frontend
res.json(payload)
} catch(error) {
if (error instanceof MongoServerError) {
console.error(`There is an error: ${error}`)
}
throw error
} finally {
// Disconnect
await mongo.close()
console.log('Disconnected from DB successfully')
}
})
// GET Definitions endpoint
app.get('/api/users/:clientId/get-definitions', async (req, res) => {
const clientId = req.params.clientId
// Connect to DB
await mongo.connect()
console.log('Connected to DB successfully')
try {
// get data from db
const getDefinitions = await db.collection('definitions')
.find({ clientId: clientId })
.toArray()
// Send a response to frontend
res.json(getDefinitions) // Added zero fetches the first object in the array
} catch (error) {
console.error(error)
} finally {
// Disconnect
await mongo.close()
console.log('Disconnected from DB successfully')
}
})
// app.listen(3000, () => { console.log('Server is listening on port 3000') }) // replaced by ViteExpress
ViteExpress.listen(app, 3000, () =>
console.log("Server is listening on:\n\nhttp://localhost:3000\n\n"),
);