Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Express validator #24

Open
wants to merge 5 commits into
base: express-validator
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ npm install
Inject the `database/setup-database.sql` file into MySQL with this command (provide your root password when asked to):

```
mysql -uroot < database/setup-database.sql
mysql -uroot -p < database/setup-database.sql
```

## 4. Create and edit the `.env` file
Expand Down
53 changes: 29 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,37 @@ app.get('/api/users', (req, res) => {
});
});

app.post('/api/users', [
// email must be valid
check('email').isEmail(),
// password must be at least 8 chars long
check('password').isLength({ min: 8 }),
// let's assume a name should be 2 chars long
app.post(
'/api/users',
check('name').isLength({ min: 2 }),
],
(req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
// send an SQL query to get all users
return connection.query('INSERT INTO user SET ?', req.body, (err, results) => {
if (err) {
// If an error has occurred, then the client is informed of the error
return res.status(500).json({
error: err.message,
sql: err.sql,
});
check('email').isEmail(),
// password must be at least 5 chars long
check('password').isLength({ min: 5 }),
(req, res) => {
// Finds the validation errors in this request and wraps them in an object with handy functions
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
// If everything went well, we send the result of the SQL query as JSON
return res.json(results);
});
});

// send an SQL query to get all users
return connection.query(
'INSERT INTO user SET ?',
req.body,
(err, results) => {
if (err) {
// If an error has occurred, then the client is informed of the error
return res.status(500).json({
error: err.message,
sql: err.sql,
});
}
// If everything went well, we send the result of the SQL query as JSON
return res.json(results);
},
);
},
);

app.listen(process.env.PORT, (err) => {
if (err) {
Expand Down
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
<<<<<<< HEAD
"express-validator": "^6.9.0",
=======
"express-validator": "^6.3.1",
>>>>>>> 804aaea71aa7571dd976d3408e3c1007789f0618
"mysql": "^2.17.1"
},
"devDependencies": {
Expand Down