Skip to content

Commit 18d91c9

Browse files
authored
Merge pull request #1 from codingthat/tutorial-updates
Tutorial updates
2 parents 1e205a5 + 2fdb324 commit 18d91c9

File tree

8 files changed

+26
-36
lines changed

8 files changed

+26
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
**/node_modules
33
data
4+
yarn.lock

README.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
# REST API Tutorial
22

3-
This sample is published as part of the blog article at www.toptal.com/blog:
4-
5-
- https://www.toptal.com/nodejs/secure-rest-api-in-nodejs
6-
7-
Visit www.toptal.com/blog and subscribe to our newsletter to read great posts
3+
This sample is published as part of [the corresponding article](https://www.toptal.com/nodejs/secure-rest-api-in-nodejs) at the Toptal Engineering Blog. Visit https://www.toptal.com/developers/blog and subscribe to our newsletter to read great posts!
84

95
## Before using
106

117
- Please make sure that you have:
12-
- node.js installed (https://nodejs.org/)
13-
- have mongodb installed and running locally (https://www.mongodb.com/)
8+
- Node.js installed (https://nodejs.org/)
9+
- MongoDB installed and running locally (https://www.mongodb.com/)
1410
- Using Windows, just open the terminal at where you installed mongo and run `mongod.exe`
15-
- run npm install in your root project folder
11+
- Run `npm install` or `yarn` in your root project folder
12+
1613
## Usage
1714

1815
To run the project, please use a command line the following:
19-
- npm start
16+
- `npm start`
2017
- It will run the server at port 3600.
2118

2219

@@ -35,3 +32,11 @@ If you are familiar to docker and you have docker installed on your machine and
3532
### 2020-02-01
3633

3734
I've created a 2020 version of this project using Typescript. If you might be interested on it, please check the following repository: https://github.com/makinhs/expressjs-api-tutorial
35+
36+
### 2020-09-09
37+
38+
- Updated and pruned dependencies.
39+
- Fixed deprecation warnings.
40+
- Leveraged `findOneAndUpdate` to simplify PATCH code.
41+
- Changed default MongoDB server name to `localhost` to simplify first-time setup.
42+
- Checked that it works with the latest version of Node.js, 14.9.0.

authorization/controllers/authorization.controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ exports.login = (req, res) => {
1010
let hash = crypto.createHmac('sha512', salt).update(refreshId).digest("base64");
1111
req.body.refreshKey = salt;
1212
let token = jwt.sign(req.body, jwtSecret);
13-
let b = new Buffer(hash);
13+
let b = Buffer.from(hash);
1414
let refresh_token = b.toString('base64');
1515
res.status(201).send({accessToken: token, refreshToken: refresh_token});
1616
} catch (err) {

common/middlewares/auth.validation.middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ exports.verifyRefreshBodyField = (req, res, next) => {
1111
};
1212

1313
exports.validRefreshNeeded = (req, res, next) => {
14-
let b = new Buffer(req.body.refresh_token, 'base64');
14+
let b = Buffer.from(req.body.refresh_token, 'base64');
1515
let refresh_token = b.toString();
1616
let hash = crypto.createHmac('sha512', req.jwt.refreshKey).update(req.jwt.userId + secret).digest("base64");
1717
if (hash === refresh_token) {

common/services/mongoose.service.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@ let count = 0;
33

44
const options = {
55
autoIndex: false, // Don't build indexes
6-
reconnectTries: 30, // Retry up to 30 times
7-
reconnectInterval: 500, // Reconnect every 500ms
86
poolSize: 10, // Maintain up to 10 socket connections
97
// If not connected, return errors immediately rather than waiting for reconnect
108
bufferMaxEntries: 0,
11-
//geting rid off the depreciation errors
9+
// all other approaches are now deprecated by MongoDB:
1210
useNewUrlParser: true,
1311
useUnifiedTopology: true
1412

1513
};
1614
const connectWithRetry = () => {
1715
console.log('MongoDB connection with retry')
18-
mongoose.connect("mongodb://mongo:27017/rest-tutorial", options).then(()=>{
16+
mongoose.connect("mongodb://localhost:27017/rest-tutorial", options).then(()=>{
1917
console.log('MongoDB is connected')
2018
}).catch(err=>{
2119
console.log('MongoDB connection unsuccessful, retry after 5 seconds. ', ++count);

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ app.use(function (req, res, next) {
1414
res.header('Access-Control-Expose-Headers', 'Content-Length');
1515
res.header('Access-Control-Allow-Headers', 'Accept, Authorization, Content-Type, X-Requested-With, Range');
1616
if (req.method === 'OPTIONS') {
17-
return res.send(200);
17+
return res.sendStatus(200);
1818
} else {
1919
return next();
2020
}

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@
2121
"body-parser": "1.19.0",
2222
"express": "^4.17.1",
2323
"jsonwebtoken": "^8.5.1",
24-
"moment": "^2.24.0",
25-
"moment-timezone": "^0.5.27",
26-
"mongoose": "^5.7.9",
27-
"uuid": "^3.3.3",
28-
"swagger-ui-express": "^4.1.2",
29-
"sync-request": "^6.1.0"
24+
"mongoose": "^5.10.3",
25+
"uuid": "^8.3.0"
3026
}
3127
}

users/models/users.model.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,14 @@ exports.list = (perPage, page) => {
5959
};
6060

6161
exports.patchUser = (id, userData) => {
62-
return new Promise((resolve, reject) => {
63-
User.findById(id, function (err, user) {
64-
if (err) reject(err);
65-
for (let i in userData) {
66-
user[i] = userData[i];
67-
}
68-
user.save(function (err, updatedUser) {
69-
if (err) return reject(err);
70-
resolve(updatedUser);
71-
});
72-
});
73-
})
74-
62+
return User.findOneAndUpdate({
63+
_id: id
64+
}, userData);
7565
};
7666

7767
exports.removeById = (userId) => {
7868
return new Promise((resolve, reject) => {
79-
User.remove({_id: userId}, (err) => {
69+
User.deleteMany({_id: userId}, (err) => {
8070
if (err) {
8171
reject(err);
8272
} else {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy