Description Having a working database allows us to build a simple REST API. A very simple API can allow us to list existing Users, create a new User (registration) and edit an existing User.
Let's build the three needed API Endpoints (see Goals) which interact with the underlying MongoDB database, by using the Express router express.Router()
.
As all REST APIs, proper HTTP status codes must be used in the server responses.
Do not forget to hash user passwords before storing it into the database. One of the most widely used hashing algorithm is bcrypt.
Lastly, optionally use the new ES6 async / await feature in all Services files. It is often simpler to use than pure Promises and allows to use a more traditional syntax.
Goals
- A
(GET) /users
API Endpoint must be available to retrieve the list of users - A
(POST) /users
API Endpoint must be available to create a new User and return it - A
(PUT) /users/:id
API Endpoint must be available to update a User and return the updated user. Only email and username can be updated.
Allowed Npm Packages
axios
: http client used to perform http requestsbcryptjs
: password hasherbody-parser
: Express middleware to parse the body requestsexpress
: web servermoment
: date managermongoose
: MongoDB clientnconf
: configuration files managervalidator
: string validation librarywinston
: logger
Requirements
-
The results must be saved in
userdata/data.json
-
The logs must be saved under
storage/logs/nodeJobs.log
-
The Data Logger must reside into
libraries/dataLogger.js
-
The File Logger must reside into
libraries/fileLogger.js
-
The MongoDB configuration variables must reside into
config/secrets.json
, which MUST be gitignored -
A
config/secrets.json.example
file must be provided, with the list of supported keys and example values of theconfig/secrets.json
file -
Configuration values must be loaded by using
nconf
directly at the beginning of theindex.js
-
The Mongoose configuration must reside into a
mongoose.js
file, loaded directly from theindex.js
-
The Mongoose client must be made available in Express under the
mongooseClient
key -
The Users Model must be saved into
models/users.js
and have the following Schema :- username: String, required, unique
- email: String, unique
- password: string, required
-
The Users Model must be made available in Express under the
usersModel
key -
The
/users
routes must be defined in theservices/users/users.router.js
file by using the Express router -
Middlewares used in the
/users
endpoints must reside in theservices/users/middlewares/
folder -
Optionally use only
async
/await
instead of pure Promises in all/services/
files -
User input validation errors must return a
422
Json response with{ hasError: 1/0
,error: <string>
} as response data (payload) -
User passwords must be
bcrypt
hashed before being saved into the database
Suggestions
- Use Postman to test the API Endpoints
- The request json body data is available under the Express
req.body
variable, once thebody-parser
middleware has been configured
// index.js
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
- Use middlewares to perform data validation, eg.
// services/users/users.router.js
[...]
const usersValidationNew = require('./middlewares/users.validation.new');
[...]
// Validation Middleware.
router.post('/', usersValidationNew);
- In order to validate user inputs, validator can be very helpful