Template for building REST APIs with Node and Koa JS
- App setup
- Linting
- Router
- Test setup
- Database setup
- Logging
- Validation setup with @hapi/joi
- Node
- MongoDB
- KoaJS
- Fork the repository
- Install dependencies using either
yarn install
ornpm install
- Setup your database and environmental variables as shown in the .env.example file
- Add your code
- Run
- From your middleware folder, create a .js file to hold your middleware
const { Joi, itemTypes, validate } = require('../utils/validator');
const registrationSchema = Joi.object({
username: Joi.string().min(4).max(30).required(),
password: Joi.string().required()
});
const validateRegistration = () => validate({
schema: registrationSchema,
itemType: itemType.body,
opt: { abortEarly: false }
});
module.exports = validateRegistration;
- import the validateRegistration middleware for the registration route
- Then add it to your route
router.post('/api/v1/signup', validateRegistration(), (ctx) => {
// Enter signup code here...
});
- create a models folder inside the
src
folder - define your mongoose models and you are good to go.
- run
npm run dev
- run
npm test