You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A simple url shortener written in Node JS using Express and MongoDB.<br>
4
-
Description: automatically redirects to saved "long" urls, counts clicks per url and has an API access point to add/edit/view/delete urls via an external app.
3
+
A simple URL shortener written in Node JS using Express and MongoDB.<br>
4
+
Description: automatically redirects to saved "long" URLs, counts clicks per URL and has an API endpoint to add, edit, view and delete URLs via an external app.
5
5
6
6
> [!TIP]
7
-
> To get the most use out of this app - have it running on a hosting service so it can be accessed from any device. Ideally you'd need a short domain as well for this app to make sense.
7
+
> To get the most use out of this app - host it on a server so it can be accessed from any device. Ideally you'd need a short domain for this app to make sense.
8
8
9
9
<details>
10
10
<summary><h3>Content</h3></summary>
11
11
12
+
-[Project Structure](#project-structure)
12
13
-[Installation](#installation)
13
-
-[API actions](#api-actions)
14
+
-[API Endpoints](#api-endpoints)
14
15
-[Parameters](#parameters)
15
16
-[Responses](#responses)
16
-
-[Registering new short url](#registering-new-short-url)
17
-
-[Editing existing urls](#editing-existing-urls)
18
-
-[Searching existing short urls](#searching-existing-short-urls)
19
-
-[Deleting existing short urls](#deleting-existing-short-urls)
17
+
-[Registering new short URL](#registering-new-short-url)
18
+
-[Editing existing URLs](#editing-existing-urls)
19
+
-[Searching existing short URLs](#searching-existing-short-urls)
20
+
-[Deleting existing short URLs](#deleting-existing-short-urls)
To edit a short url the `rb` parameter has to be the same as the user that registered it. If `<key>` is passed instead, the app will treat the user as an admin and let them bypass this check.
-`showAll` is an optional filter, when queried by an admin would return pairs of all users instead of only the user's.<br>
173
+
(has no effect if user isn't an admin)
120
174
121
-
Searching will by default only return urls registered by the user passed in `rb`, unless `<key>` was passed, then the app will treat the user as an admin and let them bypass this check.
175
+
Returns an array of matching pairs.
122
176
123
-
### act=search
177
+
##Deleting existing short URLs
124
178
125
-
Required parameters:
179
+
**DELETE**`/api/urlpairs/:page`
126
180
127
-
```Javascript
128
-
GET<urlRemote>api?key=<key>&act=search&rb=<user>
181
+
**Body:**
182
+
183
+
```json
184
+
{
185
+
"label": "Example",
186
+
"registeredBy": "user123"
187
+
}
129
188
```
130
189
131
-
Optional parameters:
190
+
-`registeredBy` is required and must match the owner of the URL pair or be an admin.
> The app will return an array of objects whose fields included the search parameters passed in the request.
198
+
> Only admins can refresh the database.
199
+
200
+
> [!IMPORTANT]
201
+
> The database is refreshed automatically when accessed via the API. Use this action only if you updated the database manually.
141
202
142
-
## Deleting existing short urls
203
+
#Creating short URLs directly inside MongoDB
143
204
144
-
To delete a short url the `rb` parameter has to be the same as the user that registered it. If `<key>` is passed instead, the app will treat the user as an admin and let them bypass this check.
205
+
The documents need to match the schema in `schemas/urlPair.js`.
To refresh the database the `rb` parameter has to be equal to `<key>`.<br>
219
+
After adding documents directly to MongoDB, restart or refresh the app via the API.
155
220
156
-
> [!IMPORTANT]
157
-
> If accessed via the API, the database gets refreshed automatically, this action is only necessary if the database was updated manually via MongoDB.
221
+
# Webhook logs
158
222
159
-
### act=refresh
223
+
The app has a basic logs system that can optionally send events to a Discord channel (or any other service that accepts webhooks).<br>
224
+
If a `WEBHOOK_URL` is set, the app will attempt to send a webhook message using the optional `APP_NAME` and `APP_AVATAR` variables.<br>
225
+
Regardles whether a webhook is set, all messages sent to the system are logged in the console.<br>
226
+
<br>
227
+
Currently the app only sends messages when it goes online/offline and database events, but feel free to add more logs using the function in `utils/webhooks.js`:
160
228
161
-
```Javascript
162
-
GET<urlRemote>api?key=<key>&act=refresh&rb=<user>
229
+
```js
230
+
constwebhookLog=require("./utils/webhooks");
231
+
awaitwebhookLog("New log message.");
163
232
```
164
233
165
-
# Creating short urls directly inside MongoDB
234
+
The function allows for a few overrides: app name and avatar overrides if you'd like to use something different from the default ones, and a message override if you'd like the message sent in the webhook to be different from the one printed in the console:
166
235
167
-
The files will need to be according to the schema in `schemas/urlpair.js`.
168
-
169
-
```
170
-
url:<The url you'd like to shorten>
171
-
page: <The url extension your short url will get>
172
-
label: <A readable name for your short url>
173
-
registeredBy: <Name or ID of the user that registered the short url>
174
-
clicks: 0
236
+
```js
237
+
constwebhookLog=require("./utils/webhooks");
238
+
awaitwebhookLog("New console log message.", "Other system", "https://myapp.com/other_system.png", "New webhook message");
175
239
```
176
240
177
-
After adding files directly to MongoDB, the app will need to be restarted or refreshed via the API.
241
+
> [!NOTE]
242
+
> The webhook's body structure and content formatting were set for my personal taste and needs, so feel free to adjust them.
178
243
179
244
# Usage
180
245
181
-
1. Register short urls via an external app that uses the API, or via creating files in your MongoDB database.
182
-
2. Use the short urls and you will be redirected to their long counterparts automatically.
246
+
1. Register short URLs via an external app that uses the API, or by creating documents in your MongoDB database.
247
+
2. Use the short URLs and you will be redirected to their long counterparts automatically.
0 commit comments