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
7b39228d
Commit
7b39228d
authored
Mar 11, 2023
by
Болатов Ален
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#2
connected to mongodb
parent
98b1cb39
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
371 additions
and
21 deletions
+371
-21
.env
backend/.env
+4
-0
package-lock.json
backend/package-lock.json
+284
-5
package.json
backend/package.json
+4
-1
index.ts
backend/src/index.ts
+13
-4
links.ts
backend/src/models/links.ts
+5
-0
links.router.ts
backend/src/routes/links.router.ts
+32
-0
linksRouter.ts
backend/src/routes/linksRouter.ts
+0
-11
database.service.ts
backend/src/services/database.service.ts
+17
-0
environment.d.ts
backend/src/types/environment.d.ts
+12
-0
No files found.
backend/.env
0 → 100644
View file @
7b39228d
DB_CONN_STRING="mongodb://localhost:27017"
DB_NAME="linksDB"
LINKS_COLLECTION_NAME="links"
PORT='3000'
\ No newline at end of file
backend/package-lock.json
View file @
7b39228d
This diff is collapsed.
Click to expand it.
backend/package.json
View file @
7b39228d
...
...
@@ -10,8 +10,11 @@
"author"
:
""
,
"license"
:
"ISC"
,
"dependencies"
:
{
"@types/node"
:
"^18.15.0"
,
"cors"
:
"^2.8.5"
,
"express"
:
"^4.18.2"
"dotenv"
:
"^16.0.3"
,
"express"
:
"^4.18.2"
,
"mongodb"
:
"^5.1.0"
},
"devDependencies"
:
{
"@types/cors"
:
"^2.8.13"
,
...
...
backend/src/index.ts
View file @
7b39228d
import
express
,
{
Express
}
from
'express'
;
import
{
linksRouter
}
from
'./routes/linksRouter'
;
import
{
linksRouter
}
from
'./routes/links.router'
;
import
{
connectToDatabase
}
from
'./services/database.service'
;
const
app
:
Express
=
express
();
app
.
use
(
express
.
json
());
app
.
use
(
'/links'
,
linksRouter
);
app
.
listen
(
3000
,
()
=>
{
console
.
log
(
`Server is listening on port 3000`
);
});
connectToDatabase
()
.
then
(()
=>
{
app
.
use
(
'/links'
,
linksRouter
);
app
.
listen
(
process
.
env
.
PORT
,
()
=>
{
console
.
log
(
`Server started at http://localhost:
${
process
.
env
.
PORT
}
`
);
});
})
.
catch
((
error
:
Error
)
=>
{
console
.
log
(
'Database connection failed'
);
process
.
exit
();
});
backend/src/models/links.ts
0 → 100644
View file @
7b39228d
import
{
ObjectId
}
from
'mongodb'
;
export
default
class
Link
{
constructor
(
public
originalUrl
:
string
,
public
id
?:
ObjectId
)
{}
}
backend/src/routes/links.router.ts
0 → 100644
View file @
7b39228d
import
express
,
{
Router
,
Request
,
Response
}
from
'express'
;
import
{
ObjectId
}
from
'mongodb'
;
import
{
collections
}
from
'../services/database.service'
;
import
Link
from
'../models/links'
;
export
const
linksRouter
:
Router
=
express
.
Router
();
linksRouter
.
get
(
'/'
,
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
res
.
send
(
'Hello'
);
}
catch
(
error
:
unknown
)
{
const
err
=
error
as
Error
;
res
.
status
(
500
).
send
(
err
.
message
);
}
});
linksRouter
.
post
(
'/'
,
async
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
const
newLink
=
req
.
body
as
Link
;
const
result
=
await
collections
.
links
?.
insertOne
(
newLink
);
if
(
result
)
{
res
.
status
(
201
)
.
send
(
`Successfully created a new link with id
${
result
.
insertedId
}
`
);
}
else
{
res
.
status
(
500
).
send
(
'Failed to create a new link.'
);
}
}
catch
(
err
:
unknown
)
{
const
error
=
err
as
Error
;
res
.
status
(
500
).
send
(
error
.
message
);
}
});
backend/src/routes/linksRouter.ts
deleted
100644 → 0
View file @
98b1cb39
import
express
,
{
Router
,
Request
,
Response
}
from
'express'
;
export
const
linksRouter
:
Router
=
express
.
Router
();
linksRouter
.
get
(
'/'
,
(
req
:
Request
,
res
:
Response
)
=>
{
res
.
send
(
'Hello'
);
});
linksRouter
.
post
(
'/'
,
(
req
:
Request
,
res
:
Response
)
=>
{
res
.
send
(
'Hello'
);
});
backend/src/services/database.service.ts
0 → 100644
View file @
7b39228d
import
*
as
mongoDB
from
'mongodb'
;
import
*
as
dotenv
from
'dotenv'
;
export
const
collections
:
{
links
?:
mongoDB
.
Collection
}
=
{};
export
const
connectToDatabase
=
async
()
=>
{
dotenv
.
config
();
const
client
:
mongoDB
.
MongoClient
=
new
mongoDB
.
MongoClient
(
process
.
env
.
DB_CONN_STRING
);
await
client
.
connect
();
const
db
:
mongoDB
.
Db
=
client
.
db
(
process
.
env
.
DB_NAME
);
const
linksCollection
:
mongoDB
.
Collection
=
db
.
collection
(
process
.
env
.
LINKS_COLLECTION_NAME
);
collections
.
links
=
linksCollection
;
console
.
log
(
'Connected to database'
);
};
backend/src/types/environment.d.ts
0 → 100644
View file @
7b39228d
declare
global
{
namespace
NodeJS
{
interface
ProcessEnv
{
PORT
:
string
;
DB_CONN_STRING
:
string
;
DB_NAME
:
string
;
LINKS_COLLECTION_NAME
:
string
;
}
}
}
export
{};
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