forked from biswajit-713/nodejs-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 078a1a4
Showing
12 changed files
with
1,145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
var express = require('express'); | ||
var router = express.Router(); | ||
var bodyParser = require('body-parser'); | ||
|
||
var request = require('request'); | ||
const config = require('../config/config.js'); | ||
|
||
var url = global.gConfig.node_host + ":" + global.gConfig.node_port; | ||
|
||
router.use(bodyParser.urlencoded({ extended: true })); | ||
router.use(bodyParser.json()); | ||
|
||
// create a user in database | ||
router.post('/', (req, res) => { | ||
request({ | ||
url: `${url}/users/`, | ||
method: "POST", | ||
json: req.body | ||
}, (error, response, body) => { | ||
if (error) { | ||
return status(500).send(`Oops!!! I'm sick right now.`); | ||
} | ||
return res.status(response.statusCode).send(body); | ||
}); | ||
}); | ||
|
||
// return all users from database | ||
router.get('/', (req, res) => { | ||
request(`${url}/users/`, {json: true}, (err, response, body) => { | ||
if (err) { | ||
return status(500).send(`Oops!!! I'm sick right now.`); | ||
} | ||
return res.status(response.statusCode).send(body); | ||
}); | ||
}); | ||
|
||
// return single user from database | ||
router.get('/:id', (req, res) => { | ||
|
||
request(`${url}/users/${req.params.id}`, {json: true}, (err, response, body) => { | ||
if (err) { | ||
return status(500).send(`Oops!!! I'm sick right now.`); | ||
} | ||
|
||
if (response.statusCode === 404) { | ||
return res.status(response.statusCode).send(body); | ||
} | ||
res.status(response.statusCode).send(body); | ||
}); | ||
|
||
}); | ||
|
||
// delete a user from database | ||
router.delete('/:id', (req, res) => { | ||
User.findByIdAndRemove(req.params.id, (err, user) => { | ||
if (err) { | ||
return res.status(500).send(`There was a problem deleting the user.`); | ||
} | ||
res.status(200).send(`User ${user.name} is deleted.`); | ||
}); | ||
}); | ||
|
||
// update a single user in the database | ||
router.put('/:id', (req, res) => { | ||
User.findByIdAndUpdate(req.params.id, re.body, { new: true }, (err, user) => { | ||
if (err) { | ||
return res.status(500).send(`There was a problem updating the user`); | ||
} | ||
res.status(200).send(user); | ||
}); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
var express = require('express'); | ||
var app = express(); | ||
var db = require('./db'); | ||
|
||
var UserController = require('./user/UserController'); | ||
app.use('/users', UserController); | ||
|
||
var AccountController = require('./account/AccountController'); | ||
app.use('/accounts', AccountController); | ||
|
||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const _ = require('lodash'); | ||
|
||
const config = require('./config.json'); | ||
const defaultConfig = config.development; | ||
const environment = process.env.NODE_ENV || 'development'; | ||
const environmentConfig = config[environment]; | ||
const finalConfig = _.merge(defaultConfig, environmentConfig); | ||
|
||
global.gConfig = finalConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"development": { | ||
"config_id": "development", | ||
"app_name": "AccountService", | ||
"app_desc": "manages the user account details", | ||
"node_host": "http://localhost", | ||
"node_port": 3000, | ||
"user_endpoint": "/users/", | ||
"json_indentation": 4, | ||
"database": "mongodb://biswajit:IcIc1pwd@ds123963.mlab.com:23963/mongo-sandbox" | ||
}, | ||
"testing": { | ||
"config_id": "testing" | ||
}, | ||
"production": { | ||
"config_id": "production" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var mongoose = require('mongoose'); | ||
const config = require('./config/config.js'); | ||
|
||
mongoose.connect(global.gConfig.database, { | ||
useNewUrlParser: true | ||
}); |
Oops, something went wrong.