Express App Generator¶
Boilerplate and tooling for authoring data API backends with Node.js, JWT and MongoDB. It is best suited for developing a es6 API endpoint as a standalone (micro)service (demo), backing up web front-ends and/or mobile apps. This Generator will help to us create a express api application skeleton along with these keys features
- components Style coding
- Eslint Standard style guide
- JWT authentication
- Test cases with Mocha
- Docker
- Swagger API doc
Requirements¶
- Node 10.15.1+
- MongoDB 3.4+
- yeoman-generator 3.2.0+
Contents¶
Stepup ENV¶
Steps to create a node application skeleton:
$ npm install -g yo
$ npm install -g generator-node-api-boilerplate
$ yo node-api-boilerplate
Now the app was created and start running in your system
Usage¶
start the express server:
$ npm start
Test The unit testcases:
$ npm test
clean build folder and compile the source code:
$ npm run start:dev
find Lint issues in source code:
$ npm run lint
find Lint issues in test cases:
$ npm run lint:test
Note
npm run lint:test:fix
and npm run lint:fix
used to fix the lint errors
Project Structure¶
Your project will look like this:
├── package.json
├── public
│ ├── logs
│ | └── access.log
| ├── docs
| | └── swagger.json
| |
├── README.md
├── src
│ ├── app
│ │ └── auth
│ │ ├── AuthRoute.js
│ │ ├── UserController.js
│ │ ├── UserMiddlerware.js
│ │ ├── UserModel.js
│ │ ├── UserRoute.js
│ │ └── UserService.js
│ ├── config
│ │ ├── db
│ │ │ └── connection.js
│ │ ├── environments
│ │ │ ├── config.js
│ │ │ ├── development.js
│ │ │ ├── index.js
│ │ │ ├── production.js
│ │ │ └── test.js
│ │ ├── express-middleware.js
│ │ ├── logger.js
│ │ └── route
│ │ ├── route.index.js
│ │ └── routes.js
│ └── server.js
├── test
│ ├── index.js
│ ├── shared.spec.js
│ └── users
│ └── users.spec.js
src¶
src is the source code directory:
├── src
│ ├── app
│ │
│ ├── config
│ │
│ └── server.js
app
contains all needed componentsconfig
has all the configurations likeDB
,ENV
server.js
is the app boostrap file
Structure your solution by components¶
Instead of MVC
pattern we recommended components based pattern:
├── src
│ ├── app
│ │ └── auth
│ │ ├── AuthRoute.js
│ │ ├── UserController.js
│ │ ├── UserMiddlerware.js
│ │ ├── UserModel.js
│ │ ├── UserRoute.js
│ │ └── UserService.js
auth
is the component name and that containsroute
,Controller
,middleware
,model
, and theservice
files.
config¶
Config folder structure:
├── src | ├── config │ │ ├── db │ │ │ └── connection.js │ │ ├── environments │ │ ├── express-middleware.js │ │ ├── logger.js │ │ └── route
DB
holds the information of MongoDB connectionenvironments
use to define ENV variables. We can set different values for different ENV.express-middleware
deines express middleware need to run this applogger.js
logger details are defined hereroute
creating interface between our components (indeside src/app ) and application.
├── src
| ├── config
│ │ ├── environments
│ │ │ ├── config.js
│ │ │ ├── development.js
│ │ │ ├── index.js
│ │ │ ├── production.js
│ │ │ └── test.js
config.js¶
It holds common ENV variables across all environments. development
, production
, and the``test`` are extends this file.
route¶
├── src
| ├── config
│ │ └── route
│ │ ├── route.index.js
│ │ └── routes.js
Need to link our components in routes.js like this
import UserRoute from '../../app/auth/UserRoute';
const Routes = [
{
url: 'users',
route: UserRoute,
gaurd: false
}
];
export default Routes;
url
group name of api endpointroute
component route objectgaurd
(optional) if you want to skip JWT verification set false. By default it sets true
API¶
Integrated `swagger API Doc <https://www.npmjs.com/package/swagger-ui-express >`_. By default it loads in root path.
You can change the API doc path in code (/src/config/express-middleware.js).
[swagger] () {
this.exApp.use('/', swaggerUi.serve, swaggerUi.setup(swaggerDocument, {}));
}