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
2ddbc097
Commit
2ddbc097
authored
Nov 30, 2020
by
Vadim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
конец занятия №75
parent
8c708bd8
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
41 additions
and
6 deletions
+41
-6
db.json
app/db/db.json
+1
-1
fileDb.js
app/db/fileDb.js
+2
-1
products.js
app/products.js
+9
-3
package-lock.json
package-lock.json
+19
-0
package.json
package.json
+3
-1
server.js
server.js
+7
-0
No files found.
app/db/db.json
View file @
2ddbc097
[{
"title"
:
"Новый продукт2"
,
"price"
:
100
,
"description"
:
"Product description"
},{
"title"
:
"Новый продукт3"
,
"price"
:
100
,
"description"
:
"Product description"
}]
\ No newline at end of file
[{
"title"
:
"Новый продукт2"
,
"price"
:
100
,
"description"
:
"Product description"
,
"_id"
:
"po07wU69_bGgD9lJaGBoB"
},{
"title"
:
"Новый продукт3"
,
"price"
:
100
,
"description"
:
"Product description"
,
"_id"
:
"po07wU69_bGgD9lJaGBoA"
},{
"title"
:
"Название продукта3"
,
"price"
:
100
,
"description"
:
"Описание продукта3"
,
"_id"
:
"po07wU69_bGgD9lJaGBoR"
}]
\ No newline at end of file
app/db/fileDb.js
View file @
2ddbc097
const
fs
=
require
(
'fs'
);
const
fileName
=
'./db/db.json'
;
const
fileName
=
'./
app/
db/db.json'
;
let
data
=
[];
module
.
exports
=
{
...
...
@@ -9,6 +9,7 @@ module.exports = {
const
fileContents
=
fs
.
readFileSync
(
fileName
,
'utf-8'
);
data
=
JSON
.
parse
(
fileContents
);
}
catch
(
e
)
{
console
.
error
(
e
);
data
=
[]
}
},
...
...
app/products.js
View file @
2ddbc097
const
express
=
require
(
'express'
);
const
router
=
express
.
Router
();
const
db
=
require
(
'./db/fileDb'
);
const
{
nanoid
}
=
require
(
'nanoid'
)
db
.
init
();
router
.
get
(
'/'
,
(
req
,
res
)
=>
{
res
.
send
(
'List of products will be here'
)
const
products
=
db
.
getItems
();
res
.
send
(
products
)
});
router
.
get
(
'/:id'
,
(
req
,
res
)
=>
{
...
...
@@ -14,8 +16,12 @@ router.get('/:id', (req, res) => {
router
.
post
(
'/'
,
(
req
,
res
)
=>
{
console
.
log
(
'Create product'
,
req
.
body
);
db
.
addItem
(
req
.
body
);
res
.
send
(
'Create product will be here'
)
const
newProduct
=
{
...
req
.
body
,
_id
:
nanoid
()
};
db
.
addItem
(
newProduct
);
res
.
send
(
newProduct
)
});
module
.
exports
=
router
;
\ No newline at end of file
package-lock.json
View file @
2ddbc097
...
...
@@ -330,6 +330,15 @@
"resolved"
:
"https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz"
,
"integrity"
:
"sha1-4wOogrNCzD7oylE6eZmXNNqzriw="
},
"cors"
:
{
"version"
:
"2.8.5"
,
"resolved"
:
"https://registry.npmjs.org/cors/-/cors-2.8.5.tgz"
,
"integrity"
:
"sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g=="
,
"requires"
:
{
"object-assign"
:
"^4"
,
"vary"
:
"^1"
}
},
"crypto-random-string"
:
{
"version"
:
"2.0.0"
,
"resolved"
:
"https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-2.0.0.tgz"
,
...
...
@@ -855,6 +864,11 @@
"integrity"
:
"sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
,
"dev"
:
true
},
"nanoid"
:
{
"version"
:
"3.1.18"
,
"resolved"
:
"https://registry.npmjs.org/nanoid/-/nanoid-3.1.18.tgz"
,
"integrity"
:
"sha512-rndlDjbbHbcV3xi+R2fpJ+PbGMdfBxz5v1fATIQFq0DP64FsicQdwnKLy47K4kZHdRpmQXtz24eGsxQqamzYTA=="
},
"negotiator"
:
{
"version"
:
"0.6.2"
,
"resolved"
:
"https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz"
,
...
...
@@ -899,6 +913,11 @@
"integrity"
:
"sha512-2s47yzUxdexf1OhyRi4Em83iQk0aPvwTddtFz4hnSSw9dCEsLEGf6SwIO8ss/19S9iBb5sJaOuTvTGDeZI00BQ=="
,
"dev"
:
true
},
"object-assign"
:
{
"version"
:
"4.1.1"
,
"resolved"
:
"https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz"
,
"integrity"
:
"sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM="
},
"on-finished"
:
{
"version"
:
"2.3.0"
,
"resolved"
:
"https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz"
,
...
...
package.json
View file @
2ddbc097
...
...
@@ -10,7 +10,9 @@
"author"
:
""
,
"license"
:
"
ISC
"
,
"dependencies"
:
{
"
express
"
:
"^4.17.1"
"
cors
"
:
"^2.8.5"
,
"
express
"
:
"^4.17.1"
,
"
nanoid
"
:
"^3.1.18"
},
"devDependencies"
:
{
"
nodemon
"
:
"^2.0.6"
...
...
server.js
View file @
2ddbc097
const
express
=
require
(
'express'
);
const
products
=
require
(
'./app/products'
);
const
cors
=
require
(
'cors'
)
const
app
=
express
();
const
port
=
8000
;
const
corsOptions
=
{
origin
:
'http://localhost:3000'
,
optionsSuccessStatus
:
200
// some legacy browsers (IE11, various SmartTVs) choke on 204
}
app
.
use
(
cors
(
corsOptions
))
app
.
use
(
express
.
json
())
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