title: Cannot POST /api/users/register
body: <p>I'm following a Udemy MERN stack course and I've got stuck in middle of a problem. There is no point in completing it before solving this problem, and in my searches I've found similar problems but a future point of the development. I just added the user registration and was checking it with postman, which is section 10 in the picture:</p>
  
  <p><img src="https://i.stack.imgur.com/BgXP8.jpg" alt="enter image description here"></p>
  
  <p>So everything works up to setting up a basic api which was the section 9 and had this result</p>
  
  <p><img src="https://i.stack.imgur.com/ETKm9.jpg" alt="enter image description here"></p>
  
  <p>I checked my code with the instructor's code and replaced some of it with his but I get the same error even when I run the code instructor has posted too. I have no idea what causes this.</p>
  
  <p><img src="https://i.stack.imgur.com/M2xrn.jpg" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/zMhRS.jpg" alt="enter image description here"></p>
  
  <p>server.js </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const express = require('express');
  const mongoose = require('mongoose');
  const bodyParser = require('body-parser');
  const users = require('./routes/api/users');
  const profile = require('./routes/api/profile');
  const posts = require('./routes/api/posts');
  const app = express();
  // Body parser 
  middlewareapp.use(bodyParser.urlencoded({ extended: false }));
  app.use(bodyParser.json());
  // // DB Config
  const db = require('./config/keys').mongoURI;
  // Connect to MongoDB
  mongoose
      .connect(db)
      .then(() =&gt; console.log('MongoDB Connected'))
  .catch(err =&gt; console.log(err));
  app.get('/', (req, res)=&gt; res.send('Hello World!'));
  // Use Routes
  app.use('/api/users', users);
  app.use('/api/profile', profile);
  app.use('/api/posts', posts);
  const port = process.env.PORT || 5001;
  app.listen(port, () =&gt; console.log(`Server running on port ${port}`));</code></pre>
  </div>
  </div>
  </p>
  
  <p>keys.js </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>module.exports = {
      mongoURI: 'mongodb://udemy1:udemy1@ds145921.mlab.com:45921/udemydev1'
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>users.js</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const express = require('express');
  const router = express.Router();
  const gravatar = require('gravatar');
  const bcrypt = require('bcryptjs');
  //Load User model
  const User = require('../../models/User')
  // @route   GET api/users/test
  // @desc    Tests users route
  // @access  Public
  router.get('/test', (req,res) =&gt; res.json({msg: "Users works"}));
  // @route   GET api/users/register
  // @desc    Register users
  // @access  Public
  router.post('register', (req, res) =&gt; {
      User.findOne({ email: req.body.email })
       .then(user =&gt; { 
         if (user) {
              return res.status(400).json({ email: 'Email already exists'});
          } else { 
             const avatar = gravatar.url(req.body.email,{
                  s: '200', //size 
                 r: 'pg', //rating 
                 d: 'mm' //default   
           }); 
             const newUser = new User ({  
               name: req.body.name,        
         email: req.body.email,          
       avatar,       
          password: req.body.password
              }); 
             bcrypt.genSalt(10, (err, salt) =&gt; { 
                 bcrypt.hash(newUser.password, salt, (err, hash) =&gt; {
                      if (err) throw err;   
                   newUser.password = hash; 
                     newUser.save()
                          .then(user =&gt; res.json(user)) 
                         .catch(err =&gt; console.log(err));
                  })
              })  
        }  
    })
  });
  module.exports = router;</code></pre>
  </div>
  </div>
  </p>
  
  <p>posts.js</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const express = require('express');
  const router = express.Router();
  // @route   GET api/posts/test
  // @desc    Tests posts route
  // @access  Public
  router.get('/test', (req,res) =&gt; res.json({msg: "Posts works"}));
  module.exports = router;</code></pre>
  </div>
  </div>
  </p>
  
  <pre><code>  profiles.js
  </code></pre>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const express = require('express');
  const router = express.Router();
  // @route   GET api/profile/test
  // @desc    Tests profile route
  // @access  Public
  router.get('/test', (req,res) =&gt; res.json({msg: "Profile works"}));
  module.exports = router;</code></pre>
  </div>
  </div>
  </p>
  
  <pre><code>User.js
  </code></pre>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const mongoose = require('mongoose');
  const Schema = mongoose.Schema;
  //create schema
  const UserSchema = new Schema({
      name: {
          type: String,
          required: true
      },
      email: {
          type: String,
          required: true
      },
      password: {
          type: String,
          required: true
      },
      avatar: {
          type: String
      },
      date: {        type: Date,
          default: Date.now
      }});
  module.exports = User = mongoose.model('users', UserSchema);</code></pre>
  </div>
  </div>
  </p>
  
  <p>package.json</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>{
    "name": "dev",
    "version": "1.0.0",
    "description": "",
    "main": "server.js",
    "scripts": {
      "start": "node server.js",
      "server": "nodemon server"
    },
    "author": "Ali Najdi",
    "license": "MIT",
    "dependencies": {
      "bcryptjs": "^2.4.3",
      "body-parser": "^1.18.3",
      "express": "^4.16.3",
      "gravatar": "^1.6.0",
      "jsonwebtoken": "^8.3.0",
      "mongoose": "^5.2.5",
      "passport": "^0.4.0",
      "passport-jwt": "^4.0.0",
      "validator": "^10.4.0"
    },
    "devDependencies": {
      "nodemon": "^1.18.3"
    }
  }</code></pre>
  </div>
  </div>
  </p>