Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
S
shop-api-js5
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
Vadim
shop-api-js5
Commits
9996b974
Commit
9996b974
authored
Dec 21, 2020
by
Vadim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
конец занятия №81
parent
4481e41e
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
290 additions
and
24 deletions
+290
-24
fileDb.js
app/db/fileDb.js
+3
-0
mongoDB.js
app/db/mongoDB.js
+20
-0
Products.js
app/models/Products.js
+22
-0
products.js
app/products.js
+23
-8
package-lock.json
package-lock.json
+211
-4
package.json
package.json
+2
-0
server.js
server.js
+9
-12
No files found.
app/db/fileDb.js
View file @
9996b974
...
...
@@ -12,6 +12,9 @@ module.exports = {
console
.
error
(
e
);
data
=
[]
}
},
getItem
(
id
){
},
getItems
()
{
return
data
;
...
...
app/db/mongoDB.js
0 → 100644
View file @
9996b974
const
MongoClient
=
require
(
'mongodb'
).
MongoClient
;
let
db
=
null
;
let
client
=
null
;
const
connect
=
async
()
=>
{
client
=
await
MongoClient
.
connect
(
"mongodb://localhost"
);
db
=
client
.
db
(
'shop'
)
};
const
disconnect
=
async
()
=>
{
await
client
.
disconnect
();
};
module
.
exports
=
{
connect
,
disconnect
,
getDb
:
()
=>
db
};
\ No newline at end of file
app/models/Products.js
0 → 100644
View file @
9996b974
const
mongoose
=
require
(
'mongoose'
);
const
Schema
=
mongoose
.
Schema
;
const
ProductSchema
=
new
Schema
({
title
:
{
type
:
String
,
required
:
true
},
price
:
{
type
:
Number
,
required
:
true
},
description
:
String
,
image
:
String
},
{
versionKey
:
false
});
const
Product
=
mongoose
.
model
(
'Product'
,
ProductSchema
);
module
.
exports
=
Product
;
app/products.js
View file @
9996b974
...
...
@@ -4,6 +4,7 @@ const path = require('path');
const
router
=
express
.
Router
();
const
{
nanoid
}
=
require
(
'nanoid'
);
const
config
=
require
(
'./config'
);
const
Product
=
require
(
'./models/Products'
);
const
storage
=
multer
.
diskStorage
({
destination
:
(
req
,
file
,
cb
)
=>
{
...
...
@@ -15,25 +16,39 @@ const config = require('./config');
});
const
upload
=
multer
({
storage
});
const
createRouter
=
(
db
)
=>
{
const
createRouter
=
()
=>
{
router
.
get
(
'/'
,
async
(
req
,
res
)
=>
{
try
{
const
products
=
await
Product
.
find
();
res
.
send
(
products
);
}
catch
(
e
)
{
res
.
status
(
500
).
send
(
e
);
}
const
products
=
await
db
.
getItems
(
'products'
);
res
.
send
(
products
);
});
router
.
get
(
'/:id'
,
async
(
req
,
res
)
=>
{
const
product
=
await
db
.
getItem
(
'products'
,
req
.
params
.
id
);
res
.
send
(
product
[
0
]);
try
{
const
result
=
await
Product
.
findById
(
req
.
params
.
id
);
res
.
send
(
result
);
}
catch
(
e
)
{
res
.
sendStatus
(
404
);
}
});
router
.
post
(
'/'
,
upload
.
single
(
'image'
),
async
(
req
,
res
)
=>
{
const
product
=
req
.
body
;
const
product
=
new
Product
(
req
.
body
)
;
if
(
req
.
file
)
{
product
.
image
=
req
.
file
.
filename
;
}
const
newProduct
=
await
db
.
addItem
(
'products'
,
product
);
res
.
send
(
newProduct
);
try
{
await
product
.
save
();
res
.
send
(
product
);
}
catch
(
e
)
{
res
.
status
(
500
).
send
(
e
);
}
});
return
router
;
...
...
package-lock.json
View file @
9996b974
This diff is collapsed.
Click to expand it.
package.json
View file @
9996b974
...
...
@@ -12,6 +12,8 @@
"dependencies"
:
{
"
cors
"
:
"^2.8.5"
,
"
express
"
:
"^4.17.1"
,
"
mongodb
"
:
"^3.6.3"
,
"
mongoose
"
:
"^5.11.8"
,
"
multer
"
:
"^1.4.2"
,
"
mysql
"
:
"^2.18.1"
,
"
nanoid
"
:
"^3.1.18"
...
...
server.js
View file @
9996b974
const
express
=
require
(
'express'
);
const
products
=
require
(
'./app/products'
);
const
cors
=
require
(
'cors'
);
const
mysql
=
require
(
'mysql'
);
const
config
=
require
(
'./app/config'
);
const
db
=
require
(
'./app/db/mysql'
);
const
mongoose
=
require
(
'mongoose'
);
const
app
=
express
();
const
port
=
8000
;
const
connection
=
mysql
.
createConnection
(
config
.
db
);
const
corsOptions
=
{
origin
:
'http://localhost:3000'
,
...
...
@@ -20,16 +17,16 @@ app.use(express.json());
app
.
use
(
express
.
static
(
'public'
));
connection
.
connect
(
err
=>
{
if
(
err
){
console
.
error
(
err
);
throw
err
;
}
app
.
use
(
'/products'
,
products
(
db
(
connection
)));
const
run
=
async
()
=>
{
await
mongoose
.
connect
(
"mongodb://localhost/shop"
,
{
useNewUrlParser
:
true
});
app
.
use
(
'/products'
,
products
());
app
.
listen
(
port
,
()
=>
{
console
.
log
(
`Server started on port
${
port
}
`
)
});
console
.
log
(
'mysql connected!'
);
});
console
.
log
(
'mongo connected!'
);
};
run
().
catch
(
console
.
log
);
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