Problem when I want to make a post or get of users

0

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;
};
    
asked by Guillermo Noblega 23.12.2018 в 03:29
source

1 answer

0

The POST query may be poorly defined in postman.

Remember in Headers put Content-Type and on the right side application/json . Second write well the path http://127.0.0.1/users and finally add the body of the query:

{
  "username": "username1",
  "mail": "[email protected]"
  "password": "mi password 123" 
}

Remember that when selecting postman the POST method you have to go to body then select the option raw and choose JSON (application/json)

If you have a port in the URL you must also add it ( http://127.0.0.1:3000/users )

Now, if no route works for you then they are not being declared.

    
answered by 24.12.2018 в 04:44