Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
E
exam_11_back
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
zarina
exam_11_back
Commits
7aca4dca
Commit
7aca4dca
authored
Jul 25, 2020
by
Алексей Анпилогов
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#4
, добавила роутер /products
parent
7ee45ded
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
5 deletions
+66
-5
products.js
app/products.js
+63
-0
users.js
app/users.js
+1
-3
config.js
config.js
+1
-1
server.js
server.js
+1
-1
No files found.
app/products.js
View file @
7aca4dca
const
express
=
require
(
"express"
);
const
multer
=
require
(
"multer"
);
const
path
=
require
(
"path"
);
const
{
nanoid
}
=
require
(
"nanoid"
);
const
config
=
require
(
"../config"
);
const
Product
=
require
(
"../models/Product"
);
const
auth
=
require
(
"../middleware/auth"
);
const
router
=
express
.
Router
();
const
storage
=
multer
.
diskStorage
({
destination
:
(
req
,
file
,
cb
)
=>
{
cb
(
null
,
config
.
uploadPath
);
},
filename
:
(
req
,
file
,
cb
)
=>
{
cb
(
null
,
nanoid
()
+
path
.
extname
(
file
.
originalname
));
}
});
const
upload
=
multer
({
storage
});
const
createRouter
=
()
=>
{
router
.
get
(
"/"
,
async
(
req
,
res
)
=>
{
let
category
;
if
(
req
.
query
.
category
)
{
category
=
{
category
:
req
.
query
.
category
};
}
try
{
const
products
=
await
Product
.
find
(
category
).
populate
(
"category"
,
"title"
);
res
.
send
(
products
);
}
catch
(
e
)
{
res
.
sendStatus
(
500
);
}
});
router
.
get
(
"/:id"
,
async
(
req
,
res
)
=>
{
const
product
=
await
Product
.
findById
(
req
.
params
.
id
).
populate
(
"category"
);
res
.
send
(
product
);
});
router
.
post
(
"/"
,
[
upload
.
single
(
"image"
),
auth
],
async
(
req
,
res
)
=>
{
const
product
=
new
Product
(
req
.
body
);
if
(
req
.
file
)
{
product
.
image
=
req
.
file
.
filename
;
}
await
product
.
save
();
res
.
send
(
product
);
});
router
.
delete
(
"/:id"
,
async
(
req
,
res
)
=>
{
try
{
res
.
send
(
await
Product
.
findByIdAndRemove
(
req
.
params
.
id
));
}
catch
(
e
)
{
res
.
status
(
500
).
send
(
e
)}
});
return
router
;
};
module
.
exports
=
createRouter
;
app/users.js
View file @
7aca4dca
...
@@ -32,9 +32,7 @@ const createRouter = () => {
...
@@ -32,9 +32,7 @@ const createRouter = () => {
}
}
user
.
generateToken
();
user
.
generateToken
();
await
user
.
save
();
await
user
.
save
();
res
.
send
(
user
);
res
.
send
(
user
);
});
});
...
@@ -50,7 +48,7 @@ const createRouter = () => {
...
@@ -50,7 +48,7 @@ const createRouter = () => {
if
(
!
user
)
return
res
.
send
(
success
);
if
(
!
user
)
return
res
.
send
(
success
);
user
.
generateToken
();
user
.
generateToken
();
await
user
.
save
()
await
user
.
save
()
;
res
.
send
(
success
);
res
.
send
(
success
);
...
...
config.js
View file @
7aca4dca
...
@@ -6,7 +6,7 @@ module.exports = {
...
@@ -6,7 +6,7 @@ module.exports = {
rootPath
,
rootPath
,
uploadPath
:
path
.
join
(
rootPath
,
"public"
,
"uploads"
),
uploadPath
:
path
.
join
(
rootPath
,
"public"
,
"uploads"
),
db
:
{
db
:
{
name
:
"s
hop
"
,
name
:
"s
tore
"
,
url
:
"mongodb://localhost"
url
:
"mongodb://localhost"
}
}
};
};
server.js
View file @
7aca4dca
...
@@ -12,7 +12,7 @@ app.use(cors());
...
@@ -12,7 +12,7 @@ app.use(cors());
app
.
use
(
express
.
static
(
"public"
));
app
.
use
(
express
.
static
(
"public"
));
app
.
use
(
express
.
json
());
app
.
use
(
express
.
json
());
mongoose
.
connect
(
`
${
config
.
db
.
url
}
/
${
config
.
db
.
name
}
`
,
{
useNewUrlParser
:
true
})
mongoose
.
connect
(
`
${
config
.
db
.
url
}
/
${
config
.
db
.
name
}
`
,
{
useNewUrlParser
:
true
,
useUnifiedTopology
:
true
,
useCreateIndex
:
true
,
})
.
then
(()
=>
{
.
then
(()
=>
{
console
.
log
(
"Mongoose connected!"
);
console
.
log
(
"Mongoose connected!"
);
app
.
use
(
"/products"
,
products
());
app
.
use
(
"/products"
,
products
());
...
...
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