I am working on a project with Javascript, Node.js and Express. I'm trying to do all the user logic but for some reason it does not want to work. The error that postman throws at me is this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /users/</pre>
</body>
</html>
This is the model code for a user
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
_id: String,
username: { type: String, minlength: 5, maxlength: 50, required: true },
mail: {
type: String,
minlength: 5,
maxlength: 255,
required: true,
unique: true
},
password: { type: String, minlength: 5, required: true }
});
/* Create the model from the schema. */
const User = mongoose.model("User", userSchema);
module.exports = User;
Then I have a routes folder that has a users folder that has an index.js
const checkAuth = require('../../middlewares/check-auth.js');
const handlers = require('./handlers');
const validators = require('./validators');
module.exports = router => {
router.get('/users', checkAuth, validators.find, handlers.find);
router.post('/users', validators.create, handlers.create);
router.get('/users/:id', checkAuth, validators.find, handlers.findById);
router.put('/users/:id', checkAuth, validators.update, handlers.update);
router.delete(
'/users/:id',
checkAuth,
validators.deletion,
handlers.deletion,
);
return router;
};
Handlers.js
const User = require('mongoose').model('User');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const find = (req, res) => {
...
};
const findById = (req, res) => {
...
};
const generateToken = tokenData => {
...
};
const create = (req, res) => {
...
};
const update = (req, res) => {
...
};
const deletion = (req, res) => {
...
};
module.exports = {
find,
findById,
create,
update,
deletion,
};
validator.js
const { celebrate, Joi } = require('celebrate');
const find = celebrate({
...
});
const findOne = celebrate({
...
});
const create = celebrate({
...
});
const update = celebrate({ ... });
const deletion = celebrate({ ... });
module.exports = {
find,
findOne,
create,
update,
deletion,
};
And also another index to load the user route and others I have.
const markers = require("./markers");
const users = require("./users");
const resourceRoutes = [markers, users];
module.exports = router => {
resourceRoutes.forEach(routes => routes(router));
return router;
};