Skip to content

Commit 301cbbf

Browse files
committed
Initial commit
0 parents  commit 301cbbf

21 files changed

+572
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
/.idea

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## Currently In Progress...
2+
3+
# Express JS REST API with JWT Authorization
4+
5+
This is a basic implementation of a REST API using Express JS (NodeJS framework) including JWT authorization.
6+
7+
### Pre-requisites
8+
9+
You must have the following installed:
10+
* Node.JS
11+
* MongoDB
12+
* Postman (optional)
13+
14+
### Getting Started
15+
16+
After cloning / forking this repo, install all dependencies by running:
17+
18+
```sh
19+
$ npm install
20+
```
21+
22+
To be continued...

app.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict"
2+
3+
const express = require('express')
4+
const bodyParser = require('body-parser')
5+
const mongoose = require('mongoose')
6+
const config = require('./config/config')
7+
const middleware = require('./src/middleware/_common/header')
8+
9+
// DB Configuration
10+
mongoose.connect(config.database)
11+
12+
// App Configuration
13+
const app = module.exports = express()
14+
15+
const Routes = new (require('./lib/build/routes'))(app)
16+
const RouteRegister = new (require('./lib/register/routeRegister.js'))()
17+
18+
app.use(middleware.adjustHeaders)
19+
app.use(bodyParser.urlencoded({extended: true}))
20+
app.use(bodyParser.json())
21+
app.set('superSecret', config.secret)
22+
23+
// register route files
24+
Routes.build(
25+
RouteRegister.use('_auth/auth').concat(
26+
RouteRegister.use('default')
27+
))
28+
29+
// app.listen(process.env.PORT || 3000)
30+
app.listen(3000)

config/config.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
secret: 'secret',
3+
4+
/**
5+
* Origins Header Information
6+
*
7+
* All clients accessing the API should have strict access. Be sure to
8+
* explicitly list all client domains for the corresponding environment.
9+
* @type {Object}
10+
*/
11+
prod: {
12+
origins: [
13+
// 'http://your-prod-domain.com',
14+
'*' // NOT SECURE
15+
]
16+
},
17+
dev: {
18+
origins: [
19+
// 'http://your-local-domain.com',
20+
'*' // NOT SECURE
21+
]
22+
},
23+
database: 'mongodb://localhost:27017/<mongo-db-name>',
24+
version: 'v1.0'
25+
};

lib/build/routes.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"use strict"
2+
3+
const app = require('../../app')
4+
5+
class Routes {
6+
/**
7+
* Create new instance of RouteGroup class
8+
*/
9+
constructor (app) {
10+
this.app = app
11+
}
12+
13+
/**
14+
* Build routes list
15+
*/
16+
build (routeGroups) {
17+
routeGroups.forEach(function (routeGroup) {
18+
this.app.use(routeGroup.getPrefix(), routeGroup.getExpressRouter())
19+
}.bind(this))
20+
}
21+
}
22+
23+
module.exports = Routes

lib/class/routeGroup.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
"use strict"
2+
3+
const express = require('express')
4+
5+
class RouteGroup {
6+
/**
7+
* Create new instance of RouteGroup class
8+
*/
9+
constructor (options) {
10+
this.expressRouter = express.Router()
11+
this.options = Object.assign({
12+
prefix: '/',
13+
middleware: []
14+
}, options ? options : {})
15+
16+
this.useMiddleware()
17+
18+
return this
19+
}
20+
21+
getExpressRouter () {
22+
return this.expressRouter
23+
}
24+
25+
useMiddleware () {
26+
this.getMiddleware().forEach(function (middleware) {
27+
this.expressRouter.use(middleware)
28+
}.bind(this))
29+
}
30+
31+
/**
32+
* Get an array of all middleware for route group
33+
*/
34+
getMiddleware () {
35+
return this.options.middleware
36+
}
37+
38+
/**
39+
* Get the prefix for route group
40+
*/
41+
getPrefix () {
42+
return this.options.prefix
43+
}
44+
45+
/**
46+
* Create route with method GET
47+
* @param {String} route Route string
48+
* @param {Function} controllerFunction Callback to be applied to route
49+
* @return {Route} Self
50+
*/
51+
get (route, controllerFunction) {
52+
this.expressRouter.get(route, controllerFunction)
53+
return this
54+
}
55+
56+
/**
57+
* Create route with method POST
58+
* @param {String} route Route string
59+
* @param {Function} controllerFunction Callback to be applied to route
60+
* @return {Route} Self
61+
*/
62+
post (route, controllerFunction) {
63+
this.expressRouter.post(route, controllerFunction)
64+
return this
65+
}
66+
67+
/**
68+
* Create route with method PUT
69+
* @param {String} route Route string
70+
* @param {Function} controllerFunction Callback to be applied to route
71+
* @return {Route} Self
72+
*/
73+
put (route, controllerFunction) {
74+
this.expressRouter.put(route, controllerFunction)
75+
return this
76+
}
77+
78+
/**
79+
* Create route with method PATCH
80+
* @param {String} route Route string
81+
* @param {Function} controllerFunction Callback to be applied to route
82+
* @return {Route} Self
83+
*/
84+
patch (route, controllerFunction) {
85+
this.expressRouter.patch(route, controllerFunction)
86+
return this
87+
}
88+
89+
/**
90+
* Create route with method DELETE
91+
* @param {String} route Route string
92+
* @param {Function} controllerFunction Callback to be applied to route
93+
* @return {Route} Self
94+
*/
95+
delete (route, controllerFunction) {
96+
this.expressRouter.delete(route, controllerFunction)
97+
return this
98+
}
99+
}
100+
101+
module.exports = RouteGroup

lib/register/_register.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
module.exports = class Register {
4+
5+
default_path () {
6+
return '../../src/';
7+
}
8+
9+
use (file_path) {
10+
return require(this.default_path() + file_path);
11+
}
12+
13+
}
14+

lib/register/classRegister.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
3+
const Register = require('./_register');
4+
5+
module.exports = class ClassRegister extends Register {
6+
7+
default_path () {
8+
return '../class/';
9+
}
10+
11+
}
12+

lib/register/controllerRegister.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
3+
const Register = require('./_register');
4+
5+
module.exports = class ControllerRegister extends Register {
6+
7+
default_path () {
8+
return '../../src/controllers/';
9+
}
10+
11+
}
12+

lib/register/middlewareRegister.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
3+
const Register = require('./_register');
4+
5+
module.exports = class MiddlewareRegister extends Register {
6+
7+
default_path () {
8+
return '../../src/middleware/';
9+
}
10+
11+
}
12+

lib/register/modelRegister.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
3+
const Register = require('./_register');
4+
5+
module.exports = class ModelRegister extends Register {
6+
7+
default_path () {
8+
return '../../src/models/';
9+
}
10+
11+
}
12+

lib/register/routeRegister.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
3+
const Register = require('./_register');
4+
5+
module.exports = class RouteRegister extends Register {
6+
7+
default_path () {
8+
return '../../src/routes/';
9+
}
10+
11+
}
12+

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "colls-api",
3+
"version": "1.0.0",
4+
"description": "A custom Node JS API framework built on top of Express JS.",
5+
"main": "app.js",
6+
"dependencies": {
7+
"app-root-dir": "^1.0.2",
8+
"body-parser": "^1.15.2",
9+
"cors": "^2.8.1",
10+
"express": "^4.14.0",
11+
"jsonwebtoken": "^7.1.9",
12+
"mongodb": "^2.2.10",
13+
"mongoose": "^4.6.1",
14+
"node-env-file": "^0.1.8"
15+
},
16+
"devDependencies": {},
17+
"scripts": {
18+
"test": "echo \"Error: no test specified\" && exit 1"
19+
},
20+
"author": "Collier Devlin",
21+
"license": "MIT"
22+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const app = require('../../../app');
2+
const User = require('../../models/user');
3+
const jwt = require('jsonwebtoken');
4+
5+
module.exports = {
6+
/**
7+
* Login and authenicate user. Will return JWT token if user is authorized.
8+
*/
9+
store: function (req, res) {
10+
User.findOne({
11+
username: req.body.username
12+
}, function(err, user) {
13+
if (err) throw err;
14+
15+
if (!user) {
16+
res.json({ success: false, message: 'Authentication failed. User not found.' });
17+
} else if (user) {
18+
19+
if (user.password != req.body.password) {
20+
res.json({ success: false, message: 'Authentication failed. Wrong password.' });
21+
} else {
22+
23+
var token = jwt.sign(user, app.get('superSecret'), {
24+
expiresIn: 1440 // expires in 24 hours
25+
});
26+
27+
res.json({
28+
success: true,
29+
message: 'Enjoy your token!',
30+
token: token,
31+
user: {
32+
id: user._id,
33+
username: user.username
34+
}
35+
});
36+
}
37+
}
38+
});
39+
}
40+
};

0 commit comments

Comments
 (0)