Tan Tan - 1 month ago 8
Node.js Question

POST 404 (not Found) when splitting into separate user.js file

When using the $http POST method to localhost address "/users" I receive a 404 error (not found).

Code #1 (interacts with Code #2)

var express = require('express');
var app = express();
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var path = require('path');

app.use(bodyParser.json());

app.use(express.static(__dirname + '/public'));

mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost:27017/goodies", function(err) {
if(!err) {
console.log("MONGOOSE connected");
}
});

var userSchema = new mongoose.Schema({
username: String,
password: String,
email: String,
firstname: String,
lastname: String,
created: { type: Date, default: Date.now }

});

var User = mongoose.model('users', userSchema);

app.post('/users', function(req, res, next){
var newUser = new User({
firstname: req.body.firstname,
lastname: req.body.lastname,
username: req.body.username,
password: req.body.password,
email: req.body.email
});
newUser.save(function(err) {
if (err) return res.status(400).send(err);
return res.send();
});
});

app.get('*', function(req, res, next){
return res.redirect('/#' + req.originalUrl);
});

var port = process.env.PORT || 1337;

app.listen(port);


Code #2 (interacts with Code #1)

app.controller('SignupController', function($scope, $http) {

$scope.userSignup = function() {
var newUser = {
firstname: $scope.firstname,
lastname: $scope.lastname,
username: $scope.username,
password: $scope.password,
email: $scope.email
};
console.log(newUser);
$http.post('/users', newUser).success(function (data){
alert('signup success');

});
};
});


Code #3 (seperating to a user.js file)

This is where I get the 404 error mentioned above.

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');

mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost:27017/goodies", function(err) {
if(!err) {
console.log("MONGOOSE connected");
}
});

var userSchema = new mongoose.Schema({
username: String,
password: String,
email: String,
firstname: String,
lastname: String,
created: { type: Date, default: Date.now }
});

var User = mongoose.model('users', userSchema);

router.post('/users', function(req, res, next){
var newUser = new User({
firstname: req.body.firstname,
lastname: req.body.lastname,
username: req.body.username,
password: req.body.password,
email: req.body.email
});

newUser.save(function(err) {
if (err) return res.status(400).send(err);
return res.send();
});
});

module.exports = router;


When separating Code #3 to user.js I also add a few lines to Code #1.

var users = require('./routes/users.js');
app.use('/users', users);


Any way to make this work? I separate Code #3 to make it cleaner. Thanks!

Answer

Since you are telling your app to use your user.js file for the routes under '/users', you need to update your user.js's path for the POST requests to '/' otherwise it routes it to '/users/users':

user.js file

var router = require('express').Router();

//...

// change '/users' to '/'
router.post('/', function(req, res, next){
  var newUser = new User({
    firstname: req.body.firstname,
    lastname: req.body.lastname, 
    username: req.body.username,
    password: req.body.password,
    email: req.body.email 
  });

  newUser.save(function(err) {
    if (err) return res.status(400).send(err);
    return res.send();
    });
});

module.exports = router;
Comments