Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
H
hw86AlenBolatov
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Болатов Ален
hw86AlenBolatov
Commits
56616a14
Commit
56616a14
authored
Mar 11, 2023
by
Болатов Ален
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#4
added additional route for get request to shortUrl
parent
51fb545b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
2 deletions
+21
-2
index.ts
backend/src/index.ts
+21
-2
No files found.
backend/src/index.ts
View file @
56616a14
import
express
,
{
Express
}
from
'express'
;
import
express
,
{
Express
,
Request
,
Response
}
from
'express'
;
import
{
linksRouter
}
from
'./routes/links.router'
;
import
{
connectToDatabase
}
from
'./services/database.service'
;
import
{
co
llections
,
co
nnectToDatabase
}
from
'./services/database.service'
;
const
app
:
Express
=
express
();
app
.
use
(
express
.
json
());
...
...
@@ -12,6 +12,25 @@ connectToDatabase()
app
.
listen
(
process
.
env
.
PORT
,
()
=>
{
console
.
log
(
`Server started at http://localhost:
${
process
.
env
.
PORT
}
`
);
});
app
.
get
(
'/:shortUrl'
,
async
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
const
shortUrl
=
req
.
params
.
shortUrl
;
const
link
=
await
collections
.
links
?.
findOne
({
shortUrl
:
`
${
process
.
env
.
BASE_URL
}
/
${
shortUrl
}
`
,
});
if
(
link
)
{
res
.
status
(
301
).
redirect
(
link
.
originalUrl
);
}
else
{
res
.
status
(
404
).
send
(
'Not found'
);
}
}
catch
(
err
:
unknown
)
{
const
error
=
err
as
Error
;
console
.
log
(
error
);
}
});
})
.
catch
((
error
:
Error
)
=>
{
console
.
log
(
'Database connection failed'
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment