Skip to content

Commit 9211f15

Browse files
authored
Merge pull request #11 from iolufemi/dev
Dev
2 parents 5dd4567 + e0d4207 commit 9211f15

34 files changed

+970
-220
lines changed

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
- mongodb
55
- redis-server
66
env:
7-
- MONGOLAB_URL=mongodb://127.0.0.1/snipe RATE_LIMIT=10 REDIS_URL=redis://127.0.0.1/1 SECURE_MODE=true NO_CACHE=no
7+
- MONGOLAB_URL=mongodb://127.0.0.1/snipe LOG_MONGOLAB_URL=mongodb://127.0.0.1/snipelogs REDIS_URL=redis://127.0.0.1/1 SECURE_MODE=true NO_CACHE=no
88
addons:
99
apt:
1010
sources:
@@ -19,6 +19,9 @@ before_script:
1919
- npm install -g gulp mocha gulp-cli codecov istanbul
2020
install:
2121
- npm install
22+
- gulp service -n api
23+
- gulp service -n test -b http://localhost:8080 -e apis
24+
- npm start >> femi.log . &
2225
script:
2326
- >-
2427
istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
<a name="0.3.1"></a>
2+
## [0.3.1](https://github.com/EnsembleLab/api-template/compare/0.3.0...0.3.1) (2017-10-18)
3+
4+
5+
16
<a name="0.3.0"></a>
27
# [0.3.0](https://github.com/EnsembleLab/api-template/compare/0.2.0...v0.3.0) (2017-09-13)
38

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Express REST API Generator
22

3-
[![Build Status](https://travis-ci.org/iolufemi/Express-REST-API-Generator.svg?branch=dev)](https://travis-ci.org/iolufemi/Express-REST-API-Generator) [![codecov](https://codecov.io/gh/iolufemi/Express-REST-API-Generator/branch/master/graph/badge.svg)](https://codecov.io/gh/iolufemi/Express-REST-API-Generator)
3+
[![Build Status](https://travis-ci.org/iolufemi/Express-REST-API-Generator.svg?branch=dev)](https://travis-ci.org/iolufemi/Express-REST-API-Generator) [![codecov](https://codecov.io/gh/iolufemi/Express-REST-API-Generator/branch/master/graph/badge.svg)](https://codecov.io/gh/iolufemi/Express-REST-API-Generator) [![Documentation Status](https://readthedocs.org/projects/api-template/badge/?version=latest)](http://api-template.readthedocs.io/en/latest/?badge=latest)
44

55
Express REST API Generator is an Express Based API skeleton. A template for starting projects with express as an API. This project can be used for creating a RESTful API using Node JS, Express as the framework and Mongoose to interact with a MongoDB instance. Mocha is also used for running unit tests in the project.
66

@@ -51,6 +51,14 @@ Then generate your first API endpoint
5151
$ gulp service --name yourFirstEndpoint // This command will create a CRUD endpoint for yourFirstEndpoint.
5252
```
5353

54+
With the `gulp service` command, you have the option of using either Mongo DB for your database model or using an API generated by this Express Generator as a database model. To use an API as a database you can pass the `baseurl` and the `endpoint` for the API to the `gulp service `. See an example below
55+
56+
```
57+
$ gulp service --name yourEndpointWithAPIAsDB --baseurl http://localhost:8080 --endpoint users
58+
```
59+
60+
> Note: You can use -n instead of --name, -b instead of --baseurl, -e instead of --endpoint
61+
5462
Try out your new endpoint.
5563

5664
Start the app

TODO.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
| Filename | line # | TODO
33
|:------|:------:|:------
44
| app.js | 62 | Write a complete Documentation for this project
5-
| routes/index.js | 291 | Test API versioning
6-
| routes/index.js | 292 | Test rate limiting
7-
| routes/index.js | 293 | Test complete route Loader test
8-
| routes/index.js | 294 | Test _sanitizeRequestUrl middleware function
9-
| routes/index.js | 295 | Test _allRequestData middleware function for default value scenario
10-
| routes/index.js | 296 | Test _enforceUserIdAndAppId middle function for when req.body is an array
11-
| routes/index.js | 297 | Make Log requests testable and write unit tests for it
12-
| routes/index.js | 298 | Develop the route loader into a separate node module to be publish on npm
13-
| routes/index.js | 299 | Develop all services onto separate node module to be publish on npm
145
| config/index.js | 12 | Test for production and development senarios
6+
| models/index.js | 14 | Automatically generate tests with the schema structure
7+
| routes/index.js | 294 | Test API versioning
8+
| routes/index.js | 295 | Test rate limiting
9+
| routes/index.js | 296 | Test complete route Loader test
10+
| routes/index.js | 297 | Test _sanitizeRequestUrl middleware function
11+
| routes/index.js | 298 | Test _allRequestData middleware function for default value scenario
12+
| routes/index.js | 299 | Test _enforceUserIdAndAppId middle function for when req.body is an array
13+
| routes/index.js | 300 | Make Log requests testable and write unit tests for it
14+
| routes/index.js | 301 | Develop the route loader into a separate node module to be publish on npm
15+
| routes/index.js | 302 | Develop all services onto separate node module to be publish on npm
1516
| services/logger/index.js | 36 | Test Error Handler
1617
| services/queue/clock.js | 11 | work on a clock functionality so kue can support scheduled jobs
1718
| services/queue/clock.js | 12 | Use the cron package here https://www.npmjs.com/package/cron for timer

config/development.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
secureMode: process.env.SECURE_MODE || false,
88
secret: process.env.SECRET || 'lakikihdgdfdjjjdgd67264664vdjhjdyncmxuei8336%%^#%gdvdhj????jjhdghduue',
99
mongoURL: process.env.MONGOLAB_URL || 'mongodb://192.168.99.100/snipe',
10+
logMongoURL: process.env.LOG_MONGOLAB_URL || 'mongodb://192.168.99.100/snipelogs',
1011
noFrontendCaching: process.env.NO_CACHE || 'yes',
1112
frontendCacheExpiry: process.env.FRONTEND_CACHE_EXPIRY || '90',
1213
backendCacheExpiry: process.env.BACKEND_CACHE_EXPIRY || '90',
@@ -21,5 +22,6 @@ module.exports = {
2122
queueUIUsername: process.env.QUEUE_UI_USERNAME || 'admin',
2223
queueUIPassword: process.env.QUEUE_UI_PASSWORD || 'password123/',
2324
queueUIPort: process.env.QUEUE_UI_PORT || 3000,
24-
enforceUserIdAppIdDeveloperId: process.env.ENFORCE_USER_ID_APP_ID_DEVELOPER_ID || 'yes'
25+
enforceUserIdAppIdDeveloperId: process.env.ENFORCE_USER_ID_APP_ID_DEVELOPER_ID || 'no',
26+
apiDBKey: process.env.API_DB_Key || 'MDg4NWM1NTA0ZTZlNTQ5MjAzNzA1ODBlOWVkNzI3MzdlNmYxZTcyMjVkOTA3N2JjYTBhZjA0YmM0N2U4NDZkNi8vLy8vLzQ1MDY='
2527
};

config/production.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
secureMode: process.env.SECURE_MODE || true,
88
secret: process.env.SECRET || 'lakikihdgdfdjjjdgd67264660okjnbgtrdxswerfgytg373745ei8336%%^#%gdvdhj????jjhdghduue',
99
mongoURL: process.env.MONGOLAB_URL || 'mongodb://192.168.99.100/snipe',
10+
logMongoURL: process.env.LOG_MONGOLAB_URL || 'mongodb://192.168.99.100/snipelogs',
1011
noFrontendCaching: process.env.NO_CACHE || 'no',
1112
frontendCacheExpiry: process.env.FRONTEND_CACHE_EXPIRY || '90',
1213
backendCacheExpiry: process.env.BACKEND_CACHE_EXPIRY || '90',
@@ -21,5 +22,6 @@ module.exports = {
2122
queueUIUsername: process.env.QUEUE_UI_USERNAME || 'admin',
2223
queueUIPassword: process.env.QUEUE_UI_PASSWORD || 'password123/',
2324
queueUIPort: process.env.QUEUE_UI_PORT || 3000,
24-
enforceUserIdAppIdDeveloperId: process.env.ENFORCE_USER_ID_APP_ID_DEVELOPER_ID || 'yes'
25+
enforceUserIdAppIdDeveloperId: process.env.ENFORCE_USER_ID_APP_ID_DEVELOPER_ID || 'no',
26+
apiDBKey: process.env.API_DB_Key || 'MDg4NWM1NTA0ZTZlNTQ5MjAzNzA1ODBlOWVkNzI3MzdlNmYxZTcyMjVkOTA3N2JjYTBhZjA0YmM0N2U4NDZkNi8vLy8vLzQ1MDY='
2527
};

docs/index.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Express REST API Generator
2+
3+
[![Build Status](https://travis-ci.org/iolufemi/Express-REST-API-Generator.svg?branch=dev)](https://travis-ci.org/iolufemi/Express-REST-API-Generator) [![codecov](https://codecov.io/gh/iolufemi/Express-REST-API-Generator/branch/master/graph/badge.svg)](https://codecov.io/gh/iolufemi/Express-REST-API-Generator) [![Documentation Status](https://readthedocs.org/projects/express-rest-api-generato/badge/?version=latest)](http://express-rest-api-generato.readthedocs.io/en/latest/?badge=latest)
4+
5+
Express REST API Generator is an Express Based API skeleton. A template for starting projects with express as an API. This project can be used for creating a RESTful API using Node JS, Express as the framework and Mongoose to interact with a MongoDB instance. Mocha is also used for running unit tests in the project.
6+
7+
The resulting API from this project is a JSON REST API which will respond to requests over HTTP. REST Clients can, therefore, connect to the resulting REST server.
8+
9+
## What is API?
10+
11+
In computer programming, an application programming interface (API) is a set of clearly defined methods of communication between various software components. A good API makes it easier to develop a computer program by providing all the building blocks, which are then put together by the programmer. An API may be for a web-based system, operating system, database system, computer hardware or software library. Just as a graphical user interface makes it easier for people to use programs, application programming interfaces make it easier for developers to use certain technologies in building applications. - [Wikipedia](https://en.wikipedia.org/wiki/Application_programming_interface)
12+
13+
## What is REST?
14+
15+
Representational state transfer (REST) or RESTful web services is a way of providing interoperability between computer systems on the Internet. REST-compliant Web services allow requesting systems to access and manipulate textual representations of Web resources using a uniform and predefined set of stateless operations. - [Wikipedia](https://en.wikipedia.org/wiki/Representational_state_transfer)
16+
17+
> NOTE: The use of this project requires that you have a basic knowledge of using express in building a REST API. If you are a newbie, here are some awesome tutorials to get you started.
18+
19+
- [Build Node.js RESTful APIs in 10 Minutes](https://www.codementor.io/olatundegaruba/nodejs-restful-apis-in-10-minutes-q0sgsfhbd)
20+
- [Easily Develop Node.js and MongoDB Apps with Mongoose](https://scotch.io/tutorials/using-mongoosejs-in-node-js-and-mongodb-applications)
21+
- [Build a RESTful API Using Node and Express 4](https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4)
22+
23+
## Why use Express REST API Generator?
24+
25+
1. To enable you to develop REST APIs in the fastest way possible.
26+
2. To encourage endpoint versioning.
27+
3. To encourage unit testing and make it super easy to get started with writing unit tests by generating basic unit tests for generated components.
28+
4. To enforce best practice in writing javascript apps by using lint.
29+
5. To encourage good code file structure that can be easily followed by other team members, especially new team members.
30+
6. To make it easy to build secure APIs with the ability to communicate with the frontend in an encrypted fashion.
31+
7. To encourage backing up of deleted data.
32+
8. To encourage logging API requests and responses for audit purposes.
33+
9. To encourage proper Error handling and logging.
34+
10. To encourage a uniform API response format across teams.
35+
11. To make it easy to write asynchronous logic and applications using the inbuilt distributed job queue.
36+
37+
## Installation
38+
39+
To start your project with Express REST API Generator, clone the repository from GitHub and install the dependencies.
40+
41+
```
42+
$ git clone https://github.com/iolufemi/Express-REST-API-Generator.git ./yourProjectName
43+
$ cd yourProjectName
44+
$ npm install
45+
$ npm install -g mocha gulp
46+
```
47+
48+
Then generate your first API endpoint
49+
50+
```
51+
$ gulp service --name yourFirstEndpoint // This command will create a CRUD endpoint for yourFirstEndpoint.
52+
```
53+
54+
Try out your new endpoint.
55+
56+
Start the app
57+
58+
```
59+
$ npm start
60+
```
61+
by default, the app will start on `POST 8080`
62+
63+
You can change the PORT by adding a `PORT` environment variable.
64+
eg.
65+
66+
```
67+
$ PORT=6000 npm start
68+
```
69+
now the app will start on `PORT 6000`
70+
71+
To start the app for development, run
72+
73+
```
74+
$ gulp
75+
```
76+
This will automatically restart your app whenever a change is detected.
77+
78+
You will now be able to access CRUD (create, read, update and delete) endpoints
79+
80+
`[POST] http://localhost:8080/yourFirstEndpoint` Create yourFirstEndpoint resources
81+
`[GET] http://localhost:8080/yourFirstEndpoint` Get yourFirstEndpoint resources. Supports limits, sorting, pagination, select (projection), search and date range
82+
`[GET] http://localhost:8080/yourFirstEndpoint/:id` Get a yourFirstEndpoint resource
83+
`[PUT] http://localhost:8080/yourFirstEndpoint` Update yourFirstEndpoint resources
84+
`[PATCH] http://localhost:8080/yourFirstEndpoint/:id` Update one yourFirstEndpoint resource
85+
`[DELETE] http://localhost:8080/yourFirstEndpoint?key=value` Delete yourFirstEndpoint resources
86+
`[DELETE] http://localhost:8080/yourFirstEndpoint/:id` Delete one yourFirstEndpoint resource
87+
`[POST] http://localhost:8080/yourFirstEndpoint/:id/restore` Restore a previously deleted yourFirstEndpoint resource
88+
89+
## Versioning your API endpoints
90+
91+
You can create multiple versions of your API endpoints by simply adding the version number to your route file name. eg. `users.v1.js` will put a version of the users resources on the `/v1/users` endpoint. users.v2.js will put a version of the users resources on the `/v2/users` endpoint. The latest version of the resources will always be available at the `/users` endpoint.
92+
93+
> NOTE: This project will automatically load route files found in the routes folder.
94+
95+
## File Structure
96+
97+
- config
98+
- controllers
99+
- models
100+
- routes
101+
- services
102+
- templates
103+
- test
104+
105+
## Getting support, Reporting Bugs and Issues
106+
107+
If you need support or want to report a bug, please log an issue [here](https://github.com/iolufemi/Express-REST-API-Generator/issues)
108+
109+
## Running Unit Tests
110+
111+
All generated endpoints come with complete test suits, we encourage you to update the tests as you extend the logic
112+
113+
```
114+
$ npm test
115+
```
116+
117+
## How to contribute
118+
119+
View how to contribute [here](https://github.com/iolufemi/Express-REST-API-Generator/blob/master/CONTRIBUTING.md)
120+
121+
## Code of Conduct
122+
123+
View the code of conduct [here](https://github.com/iolufemi/Express-REST-API-Generator/blob/master/CODE_OF_CONDUCT.md)
124+
125+
## Contributors
126+
127+
- [Olufemi Olanipekun](https://github.com/iolufemi)
128+
129+
## FAQs

gulpfile.js

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,23 @@ gulp.task('test', function() {
5454
);
5555

5656
// Remember to pass argument '--name TheServiceName' or '-n TheServiceName' to the service creation command
57+
// If you want to use an API as a database model, pass the base url and the endpoint. '--baseurl http://google.com' or '--b http://google.com'
58+
// '--endpoint users' or '--e users'
5759
// Note that the name must be singular
5860
gulp.task('service', function(){
5961
var args = argv(process.argv.slice(2));
6062
var name;
63+
var baseurl;
64+
var endpoint;
65+
baseurl = args.baseurl;
66+
endpoint = args.endpoint;
67+
if(!baseurl){
68+
baseurl = args.b;
69+
}
70+
71+
if(!endpoint){
72+
endpoint = args.e;
73+
}
6174
name = args.name;
6275
if(!name){
6376
name = args.n;
@@ -105,20 +118,37 @@ gulp.task('service', function(){
105118
});
106119

107120
// Create the Model
108-
fs.readFile('./template/model.tmpl', function(err, data){
109-
if (err){
110-
throw err;
111-
}
112-
var tpl = _.template(data);
113-
var result = tpl({service: nameCapitalise, object: nameLowerCase});
121+
if(baseurl && endpoint){
122+
fs.readFile('./template/model_api.tmpl', function(err, data){
123+
if (err){
124+
throw err;
125+
}
126+
var tpl = _.template(data);
127+
var result = tpl({baseurl: baseurl, endpoint: endpoint});
114128

115-
fs.writeFile('./models/'+nameCapitalisePlural+'.js', result, function(err){
116-
if (err){
129+
fs.writeFile('./models/'+nameCapitalisePlural+'.js', result, function(err){
130+
if (err){
131+
throw err;
132+
}
133+
console.log('Model created at ./models/'+nameCapitalisePlural+'.js');
134+
});
135+
});
136+
}else{
137+
fs.readFile('./template/model.tmpl', function(err, data){
138+
if (err){
117139
throw err;
118140
}
119-
console.log('Model created at ./models/'+nameCapitalisePlural+'.js');
141+
var tpl = _.template(data);
142+
var result = tpl({service: nameCapitalise, object: nameLowerCase});
143+
144+
fs.writeFile('./models/'+nameCapitalisePlural+'.js', result, function(err){
145+
if (err){
146+
throw err;
147+
}
148+
console.log('Model created at ./models/'+nameCapitalisePlural+'.js');
149+
});
120150
});
121-
});
151+
}
122152

123153
// Create the Model Unit Test
124154
fs.readFile('./template/model_test.tmpl', function(err, data){

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
site_name: Express REST API Generator Documentation
2+
theme: readthedocs

0 commit comments

Comments
 (0)