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.