[
  {
    "path": ".gitignore",
    "content": "node_modules\n.vscode\nnpm-debug.log\n.serverless\nvariables.env"
  },
  {
    "path": "README.md",
    "content": "# nodejs-restful-api\n![RESTful API design with Node.js](https://cdn-images-1.medium.com/max/2000/1*jjYC9tuf4C3HkHCP5PcKTA.jpeg \"RESTful API design with Node.js\")\n\nHow to create a RESTful CRUD API using Nodejs?\n\nThis tutorial will demo how to set up a bare bones \nAPI using mongodb as the database.\n\nIt consist of a User model and controller. The model\ndefines the data, and the controller will contain all \nthe business logic needed to interact with the database. \n\nIt has a db file which will be used to\nconnect the app to the database, and an app file used\nfor bootstrapping the application itself.\n\nThe server file is used to spin up the server and tells the\napp to listen on a specific port.\n\nFull tutorial can be found at:\nhttps://hackernoon.com/restful-api-design-with-node-js-26ccf66eab09#.s5l66zyeu\n"
  },
  {
    "path": "app.js",
    "content": "var express = require('express');\nvar app = express();\nvar db = require('./db');\n\nvar UserController = require('./user/UserController');\napp.use('/users', UserController);\n\nmodule.exports = app;"
  },
  {
    "path": "db.js",
    "content": "var mongoose = require('mongoose');\nmongoose.connect('mongodb://yourMongoDBURIGoesHere');"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"nodejs-creating-restful-apis\",\n  \"version\": \"1.0.0\",\n  \"description\": \"\",\n  \"main\": \"app.js\",\n  \"scripts\": {\n    \"start\": \"node server.js\",\n    \"test\": \"echo \\\"Error: no test specified\\\" && exit 1\"\n  },\n  \"author\": \"\",\n  \"license\": \"ISC\",\n  \"dependencies\": {\n    \"body-parser\": \"^1.18.2\",\n    \"express\": \"^4.16.2\",\n    \"mongoose\": \"^5.0.1\"\n  }\n}\n"
  },
  {
    "path": "server.js",
    "content": "var app = require('./app');\nvar port = process.env.PORT || 3000;\n\nvar server = app.listen(port, function() {\n  console.log('Express server listening on port ' + port);\n});"
  },
  {
    "path": "user/User.js",
    "content": "var mongoose = require('mongoose');  \nvar UserSchema = new mongoose.Schema({  \n  name: String,\n  email: String,\n  password: String\n});\nmongoose.model('User', UserSchema);\n\nmodule.exports = mongoose.model('User');"
  },
  {
    "path": "user/UserController.js",
    "content": "var express = require('express');\nvar router = express.Router();\nvar bodyParser = require('body-parser');\n\nrouter.use(bodyParser.urlencoded({ extended: true }));\nrouter.use(bodyParser.json());\nvar User = require('./User');\n\n// CREATES A NEW USER\nrouter.post('/', function (req, res) {\n    User.create({\n            name : req.body.name,\n            email : req.body.email,\n            password : req.body.password\n        }, \n        function (err, user) {\n            if (err) return res.status(500).send(\"There was a problem adding the information to the database.\");\n            res.status(200).send(user);\n        });\n});\n\n// RETURNS ALL THE USERS IN THE DATABASE\nrouter.get('/', function (req, res) {\n    User.find({}, function (err, users) {\n        if (err) return res.status(500).send(\"There was a problem finding the users.\");\n        res.status(200).send(users);\n    });\n});\n\n// GETS A SINGLE USER FROM THE DATABASE\nrouter.get('/:id', function (req, res) {\n    User.findById(req.params.id, function (err, user) {\n        if (err) return res.status(500).send(\"There was a problem finding the user.\");\n        if (!user) return res.status(404).send(\"No user found.\");\n        res.status(200).send(user);\n    });\n});\n\n// DELETES A USER FROM THE DATABASE\nrouter.delete('/:id', function (req, res) {\n    User.findByIdAndRemove(req.params.id, function (err, user) {\n        if (err) return res.status(500).send(\"There was a problem deleting the user.\");\n        res.status(200).send(\"User: \"+ user.name +\" was deleted.\");\n    });\n});\n\n// UPDATES A SINGLE USER IN THE DATABASE\nrouter.put('/:id', function (req, res) {\n    User.findByIdAndUpdate(req.params.id, req.body, {new: true}, function (err, user) {\n        if (err) return res.status(500).send(\"There was a problem updating the user.\");\n        res.status(200).send(user);\n    });\n});\n\n\nmodule.exports = router;"
  }
]