My version of the CodeWorkr Express MongoDB REST API Series.
- goto MLab and setup a databases called cars.
- create an admin user.
- npm init -y
- yarn add express
- create app.js
- add .gitignore
- add .jshintrc
- npm install dotenv --save
- create .env
- I am not adding it to git repo, look at .env.sample
Morgan : HTTP request logger middleware for node.js
- yarn add morgan
- add as middleware
app.use(logger('dev));
// Catch 440 Errors and forward them to an error handler
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// Error handler function
app.use((err, req, res, next) => {
const error = app.get('env') === 'development' ? err : {};
const status = err.status || 500;
// Respond to client
res.status(status).json({
error: {
message: error.message
}
});
// Respond to ourselves
console.error(err);
});
- Add route
- Launch server
- node app.js
- Launch Postman
- Create a New Collection
- https://www.getpostman.com/collections/6b847ee739a8d9cf6c951
- Send Get Request
- Success!
- Create a New Collection
- Create helpers folders
- Create controllers folders
- Create models folders
- yarn add nodemon
- add "start" command to package.json
- npm start
- create routes/user.js
- refactor app.js to use the user route module
- create controllers/user.js
- refactor routes/users.js to use the user controller module
- yarn add mongoose
- We can interact with mongoose in 3 different ways:
- Callbacks
- Promises
- Async/Await (Promises)
- yarn npm body-parser
- add body-parser as Middleware
- app.use(bodyParser.json());
- add users.post route
- add users.create_usrer controller
- In Postman, make sure to add Headers
- Content-Type:application/json
- The seed-db function will populate the mongodb database with starting values.
-
Refactored controller/users.js to use a Promise
-
Fixed depricated error in app.js
- mongoose.Promise = global.Promise;
- yarn add --dev nodemon
- Refactored controller/users.js to use Async/Await
- yarn add express-promise-router
so... async/await doesn't actually make Node.js code synchronous. what it does do is allow you not have to write callbacks or Promise().then().catch() functions so it makes your code verbose
so it will queue up all the calls in order, but some the async processing will return before others depending on how quickly it is processed More links
also, updated example git with file write output
- extend routes in routes/users.js, controllers/users.js
- now we have a cars route
{
"cars": [
{
"_id": "59e0d615fcdd4965849e4466",
"seller": "59e011151dc47a3a34f79440",
"make": "Ford",
"model": "Flex",
"year": 2017,
"price": 39000,
"__v": 0
},
{
"_id": "59e0d64feed04e6e80cc7d4a",
"seller": "59e011151dc47a3a34f79440",
"make": "Ford",
"model": "Flex",
"year": 2017,
"price": 39000,
"__v": 0
}
]
}
- express validator
- joi
- Object schema description language and validator for JavaScript objects.
- npm install --save joi
- validator
- node validator
- better validator
- be simple to use
- support a number of usage patterns including a fluent interface
- support re-use of validator parts
- support deep object and array validation
- be able to customise the output structure
- be able to customise failure messages
- support i18n
- use the well known validator library for string validation
- be easily used with both express.js, koa.js and koa@next
- written in and works with typescript (>= v2.0.0, see Section on Breaking Changes below)
- Go with Joi
- npm install --save joi