I am currently in the process of learning the MEAN stack by following a tutorial and have encountered an error. Unfortunately, I am having difficulty identifying exactly where I went wrong.
While attempting to test routes in Postman by creating a user, I consistently receive the message 'Cannot POST /users'.
Could someone provide assistance with this issue? Thank you in advance!
routes.js
// Dependencies
var mongoose = require('mongoose');
var User = require('./model.js');
// Opens App Routes
module.exports = function(app) {
// GET Routes
// --------------------------------------------------------
// Retrieve records for all users in the db
app.get('/users', function(req, res){
// Uses Mongoose schema to run the search (empty conditions)
var query = User.find({});
query.exec(function(err, users){
if(err)
res.send(err);
// If no errors are found, it responds with a JSON of all users
res.json(users);
});
});
// POST Routes
// --------------------------------------------------------
// Provides method for saving new users in the db
app.post('/users', function(req, res){
// Creates a new User based on the Mongoose schema and the post bo.dy
var newuser = new User(req.body);
// New User is saved in the db.
newuser.save(function(err){
if(err)
res.send(err);
// If no errors are found, it responds with a JSON of the new user
res.json(req.body);
});
});
};
model.js
// Pulls Mongoose dependency for creating schemas
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// Defines how user data is stored in the database
var UserSchema = new Schema({
username : {type: String, required: true},
gender : {type: String, required: true},
age : {type: Number, required: true},
favlang : {type: String, required: true},
location : {type: [Number], required: true}, //[Long, Lat]
htmlverified : String,
created_at : {type: Date, default: Date.now},
updated_at : {type: Date, default: Date.now}
});
// Sets the created_at parameter equal to the current time
UserSchema.pre('save', function(next){
now = new Date();
this.updated_at = now;
if(!this.created_at){
this.created_at = now
}
next();
});
// Indexes this schema in 2dsphere format
UserSchema.index({location: '2dsphere'});
// Exports the UserSchema for use elsewhere
module.exports = mongoose.model('scotch-user', UserSchema);
server.js
// Dependencies
// -----------------------------------------------------
var express = require('express');
var mongoose = require('mongoose');
var port = process.env.PORT || 3000;
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var app = express();
// Express Configuration
// -----------------------------------------------------
// Sets the connection to MongoDB
mongoose.connect("mongodb://localhost/MeanMapApp");
// Logging and Parsing
app.use(express.static(__dirname + '/public'));
app.use('/bower_components', express.static(__dirname + '/bower_components'));
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.text());
app.use(bodyParser.json({ type: 'application/vnd.api+json'}));
app.use(methodOverride());
// Routes
// ------------------------------------------------------
require('./app/routes.js')(app);
// Listen
// -------------------------------------------------------
app.listen(port);
console.log('App listening on port ' + port);