Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
H
hw87AlenBolatov
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
Болатов Ален
hw87AlenBolatov
Commits
a0e4c63e
Commit
a0e4c63e
authored
Mar 14, 2023
by
Болатов Ален
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#2
connect multer for file handling
parent
96aa4852
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
452 additions
and
47 deletions
+452
-47
package-lock.json
backend/package-lock.json
+399
-17
package.json
backend/package.json
+8
-2
index.ts
backend/src/index.ts
+4
-2
artist.ts
backend/src/routes/artist.ts
+41
-26
504fb468410c009000968b276ae8f92a
backend/uploads/504fb468410c009000968b276ae8f92a
+0
-0
No files found.
backend/package-lock.json
View file @
a0e4c63e
This diff is collapsed.
Click to expand it.
backend/package.json
View file @
a0e4c63e
...
...
@@ -11,13 +11,19 @@
"license"
:
"ISC"
,
"dependencies"
:
{
"@types/node"
:
"^18.15.3"
,
"body-parser"
:
"^1.20.2"
,
"cors"
:
"^2.8.5"
,
"dotenv"
:
"^16.0.3"
,
"express"
:
"^4.18.2"
,
"mongoose"
:
"^7.0.1"
,
"multer"
:
"^1.4.5-lts.1"
,
"ts-node-dev"
:
"^2.0.0"
},
"devDependencies"
:
{
"@types/body-parser"
:
"^1.19.2"
,
"@types/cors"
:
"^2.8.13"
,
"@types/express"
:
"^4.17.17"
,
"@types/mongoose"
:
"^5.11.97"
"@types/mongoose"
:
"^5.11.97"
,
"@types/multer"
:
"^1.4.7"
}
}
}
\ No newline at end of file
backend/src/index.ts
View file @
a0e4c63e
import
express
,
{
Express
,
json
}
from
'express'
;
import
mongoose
from
'mongoose'
;
import
'dotenv/config'
;
import
{
artistRouter
}
from
'./routes/artist'
;
import
cors
from
'cors'
;
const
app
:
Express
=
express
();
app
.
use
(
json
());
app
.
use
(
cors
());
app
.
use
(
'/artists'
,
artistRouter
);
app
.
listen
(
process
.
env
.
PORT
,
()
=>
{
console
.
log
(
'Server started on port '
+
process
.
env
.
PORT
);
console
.
log
(
'Server started on port '
+
process
.
env
.
PORT
);
});
backend/src/routes/artist.ts
View file @
a0e4c63e
import
express
,
{
Request
,
Response
}
from
'express'
;
import
{
model
,
Schema
,
connect
,
Document
,
HydratedDocument
}
from
'mongoose'
;
import
{
model
,
Schema
,
connect
,
HydratedDocument
}
from
'mongoose'
;
import
multer
from
'multer'
;
import
fs
from
'fs'
;
import
path
from
'path'
;
const
upload
=
multer
({
dest
:
'uploads/'
});
const
router
=
express
.
Router
();
interface
IArtist
{
name
:
string
;
image
?
:
File
;
description
:
string
;
name
:
string
;
image
:
File
;
description
:
string
;
}
const
artistSchema
=
new
Schema
<
IArtist
>
({
name
:
{
type
:
String
,
required
:
true
,
},
image
:
{
type
:
Buffer
,
},
description
:
{
type
:
String
,
required
:
true
,
},
name
:
{
type
:
String
,
required
:
true
,
},
image
:
{
data
:
Buffer
,
contentType
:
String
,
},
description
:
{
type
:
String
,
required
:
true
,
},
});
const
Artist
=
model
<
IArtist
>
(
'Artist'
,
artistSchema
);
const
run
=
async
()
=>
{
await
connect
(
`
${
process
.
env
.
MONGO_URL
}
/artist`
);
await
connect
(
`
${
process
.
env
.
MONGO_URL
}
/artist`
);
};
run
().
catch
((
err
)
=>
console
.
log
(
err
));
router
.
get
(
'/'
,
(
req
:
Request
,
res
:
Response
)
=>
{
return
res
.
send
(
'Hello'
);
router
.
get
(
'/'
,
async
(
req
:
Request
,
res
:
Response
)
=>
{
const
artists
=
await
Artist
.
find
();
res
.
send
(
artists
);
});
router
.
post
(
'/'
,
async
(
req
:
Request
,
res
:
Response
)
=>
{
const
artist
:
HydratedDocument
<
IArtist
>
=
new
Artist
({
name
:
req
.
body
.
name
,
description
:
req
.
body
.
description
,
});
await
artist
.
save
();
res
.
send
(
artist
);
});
router
.
post
(
'/'
,
upload
.
single
(
'image'
),
async
(
req
:
Request
,
res
:
Response
)
=>
{
const
artist
:
HydratedDocument
<
IArtist
>
=
new
Artist
({
name
:
req
.
body
.
name
,
description
:
req
.
body
.
description
,
image
:
{
data
:
fs
.
readFileSync
(
path
.
join
(
'uploads/'
+
req
.
file
?.
filename
)),
contentType
:
'image'
,
},
});
await
artist
.
save
();
res
.
send
(
'Sucessfully saved'
);
}
);
export
{
router
as
artistRouter
};
backend/uploads/504fb468410c009000968b276ae8f92a
0 → 100644
View file @
a0e4c63e
File added
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