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