0

I'm new to Node.js and considering using it as an alternative to my current DOT NET API. Anyway, I wrote some middleware to require basic non-role authorization for my app and I keep getting compilation issues with my function inputs.

Compilation error:

harry@harry-pc:~/api.harry.technology$ npm start

> [email protected] start /home/harry/api.harry.technology
> node ./bin/www

/home/harry/api.harry.technology/helpers/authorize.js:7
  if (req.get('Token')) {
          ^
TypeError: Cannot read property 'get' of undefined
    at module.exports (/home/harry/api.harry.technology/helpers/authorize.js:7:11)
    ...stack trace through library

helpers/authorize.js:

var express = require('express');
var config = require('../config.json');

module.exports = (req, res, next) => {
  // console.log(req.get('Token'));
  // The following line is line 7:
  if (req.get('Token')) {
    if (jwt.verify(token, config.secret)) {
      console.log(jwt.verify(token, config.secret));
      next();
    } else {
      return res.status(401).json({ message: 'Unauthorized' });
    }
  } else {
    return res.status(401).json({ message: 'Unauthorized' });
  }
}

routes/users.js:

var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var authorize = require('../helpers/authorize');

// (other routes)

/* POST user */
router.post('/', authorize, (req, res, next) => {
  connection.query(
    'INSERT INTO `users` (`username`, `firstname`, `lastname`, `phone`, `email`, `password`) VALUES (?, ?, ?, ?, ?, ?);',
    [req.body.username, req.body.firstname, req.body.lastname, req.body.phone, req.body.email, req.body.password],
    (error, results, fields) => {
      if (error) throw error;
      res.send(results);
    });
});

What I've tried:

I've seen similar issues around the web but the solutions either don't apply or don't work. I also read through sone JavaScript docs with no luck.

5
  • Why did you edit your middleware to fix the middleware arguments? That renders your question useless now. Someone like me comes along to read your question for the first time and it doesn't line up with the answer because you've edited it since the answer was posted. Please don't edit solutions into your question and don't make edits that completely change the tenor of the question. That just ruins the usefulness of this question to others, both now and in the future and that's one of the main purposes of stackoverflow. Commented Apr 16, 2020 at 9:53
  • @jfriend00 I edited it because I made the change with the exact same error occurring. My problem was not fixed by switching the arguments if it was I would've left it there. In the original I also stated that I had tried that then got an answer telling me to try that solution. Commented Apr 16, 2020 at 10:11
  • @jfriend00 to be clear I had clearly stated under "what I've tried" that I had edited the arguments in the exact form given by the solution in my original answer. I used the edit function to clarify my question. Commented Apr 16, 2020 at 10:14
  • Well that WAS a mistake in the code you posted. Perhaps not the only mistake, but fixing that is part of the solution. Just because you might need to fix three things in order to get things to work doesn't mean you should discredit the answers that contain the first two things to fix which is what you are doing when you incorporate those into the question - making those answers seem irrelevant now. In a multiple step fix, sometimes it is necessary to ADD to your question a new version of the code that incorporates fixes, but still isn't working to show people what you're at now. Commented Apr 16, 2020 at 16:59
  • That leaves the original question intact and doesn't discredit from the helpful suggestions that make some of the fixes. And, it encourages people to add to their answers with other problems they might find rather than discourages them from participating further. I'm just saying that there are ways you can encourage rather than discourage people from helping further. Commented Apr 16, 2020 at 17:03

2 Answers 2

1

Use req.getHeader('token') Your should have sent the token in the header or use authorization header req.getHeader('authorization'); and strip out the bearer or better still use a standard middleware like passport.

See https://nodejs.org/api/http.html#http_request_getheader_name

Sign up to request clarification or add additional context in comments.

Comments

1

Good point by tksilicon above. But also it’s saying that req is undefined. Have you tried this without the middleware to make sure the request is coming through ? Or tried console logging the request itself out ?

Although we can’t see the server code it’s possible something else is going on with the request making it to the router.

Your router needs to be exported to the sever code and included as middleware in the server code so the request hits it.

Anyway some suggestions hope it helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.