Advertisement
Guest User

REST API Nodejs server files

a guest
Dec 21st, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //******* users.js: ********//
  2. var express = require('express');
  3. var router = express.Router();
  4. var mongoose = require('mongoose');
  5. var User = mongoose.model('User');
  6.  
  7.  /* When accessing http://localhost:3000/users/ */
  8. router.route('/')
  9.  // create a user (accessed at POST http://localhost:3000/users/)
  10.     .post(function (req, res) { // create a new instance of the User model
  11.         new User({ // set the user's attributes (comes from the request)
  12.         firstname: req.body.firstname,
  13.         lastname: req.body.lastname,
  14.         age: req.body.age,
  15.         update_at: Date.now()
  16.     })
  17.     .save(function (err) {
  18.         if (err)
  19.             res.send(err);
  20.             res.json({message: 'User created!'});
  21.         });
  22.     })
  23.     // get all the users (accessed at GET http://localhost:8080/api/users)
  24.     .get(function (req, res) {
  25.         User.find(function (err, users) {
  26.             if (err)
  27.                 res.send(err);
  28.                 res.json(users);
  29.             });
  30.     });
  31.  
  32.  /* When accessing http://localhost:3000/users/:userId */
  33.  router.route('/:userId')
  34.  // get the user with that id (accessed at GET http://localhost:3000/users/:userId)
  35.     .get(function (req, res) {
  36.         User.findById(req.params.userId, function (err, user) {
  37.             if (err)
  38.                 res.send(err);
  39.             res.json(user);
  40.         });
  41.     })
  42.  
  43.     // update the user with this id (accessed at PUT http://localhost:3000/users/:userId)
  44.     .put(function (req, res) {
  45.         //use our user model to find the user we want
  46.         User.findById(req.params.userId, function (err, user) {
  47.             if (err) res.send(err);
  48.             if (!!req.body.firstname)
  49.                 user.firstname = req.body.firstname;
  50.             if (!!req.body.lastname)
  51.                 user.lastname = req.body.lastname;
  52.             if (!!req.body.age)
  53.                 user.age = req.body.age;
  54.  
  55.             // save the user
  56.             user.save(function (err) {
  57.                 if (err)
  58.                     res.send(err);
  59.                 res.json({message: 'User successfully updated!'});
  60.             });
  61.         });
  62.      })
  63.  
  64.      // delete the user with this id (accessed at DELETE http://localhost:3000/users/:userId)
  65.      .delete(function (req, res) {
  66.         User.remove({ _id: req.params.userId }, function (err, user) {
  67.             if (err)
  68.                 res.send(err);
  69.             res.json({message: 'Successfully deleted'});
  70.      });
  71. });
  72. module.exports = router;
  73.  
  74. //******* userDB.js: ********//
  75.  var mongoose = require('mongoose');
  76.  var Schema = mongoose.Schema ;
  77.  
  78.  var User = new Schema({
  79.     firstname: String,
  80.     lastname: String,
  81.     age: Number,
  82.     update_at: Date
  83.  });
  84.  
  85.  mongoose.model('User', User);
  86.  mongoose.connect('localhost', 'MyNodeDB');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement