node.js - `express` - app chrome postman giving always error instead of response -


please 1 me find issue here:

when trying post username , password getting error : requester.js:6211 post http://localhost:7000/api/login 403 (forbidden)send @ requester.js:6211(anonymous function) @ requester.js:4811dispatch @ jquery.min.js:3i @ jquery.min.js:3 requester.js:1056 defining mode

i don't know issue here @ all. here post man request screen shot :

enter image description here

here api file :

var user = require('../models/user'); var config = require('../../config');  var secretkey = config.secretkey;  var jsonwebtoken = require("jsonwebtoken");   function createtoken ( user ) {      var token = jsonwebtoken.sign({         _id : user.id,         name : user.name,         username: user.username     }, secretkey, {         expiresin  : "1440m"     })      return token; }  module.exports = function( app, express ) {      var api = express.router();      api.post('/signup', function( req, res ) {          var user = new user({             name : req.body.name,             username : req.body.username,             password : req.body.password         });          user.save( function( err ) {             if( err ) {                  res.send( err );                 return;              }              res.json({message : "user has been created!"});         });          api.get('/users', function( req, res ) {              user.find({}, function( err, users ) {                  if( err ) {                     res.send( err );                     return;                 }                  res.json( users );              })          });          api.post('/login', function( req, res ) {              user.findone({                 username : req.body.username             }).select('password').exec(function( err, user ) {                  if( err ) throw err;                  if(!user) {                     res.send({ message : "user doesnt exist!"})                 } else if( user ) {                      var validpassword = user.comparepassword( req.body.password );                      if(!validpassword) {                         res.send({ message : "invalid password"});                     } else {                          var token = createtoken( user );                          res.json({                             success : true,                             message : "successfuly loged in!",                             token : token                         })                      }                 }              })          })      });      //middleware      api.use(function(req, res, next) {           console.log("somebody came our app!");          var token = req.body.token || req.param('token') || req.headers['x-access-token'];          // check if token exist         if(token) {              jsonwebtoken.verify(token, secretkey, function(err, decoded) {                  if(err) {                     res.status(403).send({ success: false, message: "failed authenticate user"});                  } else {                     req.decoded = decoded;                     next();                 }             });         } else {             res.status(403).send({ success: false, message: "no token provided"});         }      });      //destination b      api.get("/", function( req, res ) {          res.json("hello world!");      })      return api;  } 

the route should 'api/signup' , not '/signup'.

the route definitions not right. instead of:

 api.post('/signup', function( req, res ) { ...         api.get('/users', function( req, res ) {    }) }) 

it should be:

 api.post('/signup', function( req, res ) { ... }) api.get('/users', function( req, res ) { ... }) 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -