I am a New Learner I am Facing This Issue in MiddleWare Everything Is Working Fine before but after I request then this Error will come I am Just Trying To console Log The Token Given In The Header or Applying Condition on Token
This Is The Exact Error ->
(node:14020) [MONGODB DRIVER] Warning: useNewUrlParser is a deprecated option: useNewUrlParser has no effect since
Node.js Driver version 4.0.0 and will be removed in the next major version
(Use `node --trace-warnings ...` to show where the warning was created)
(node:14020) [MONGODB DRIVER] Warning: useUnifiedTopology is a deprecated option: useUnifiedTopology has no effect
since Node.js Driver version 4.0.0 and will be removed in the next major version
listening on port: 4000
Connected to MongoDB Server
F:\Projects\SOCIAL-MEDIA\server\middlewares\requireUser.js:3
if(!req.headers || !req.headers.authorization || !req.header.authorization.startsWith('Bearer')){
^
TypeError: Cannot read properties of undefined (reading 'startsWith')
at module.exports (F:\Projects\SOCIAL-MEDIA\server\middlewares\requireUser.js:3:80)
at Layer.handle [as handle_request] (F:\Projects\SOCIAL-MEDIA\server\node_modules\express\lib\router\layer.js:95:5)
at next (F:\Projects\SOCIAL-MEDIA\server\node_modules\express\lib\router\route.js:149:13)
at Route.dispatch (F:\Projects\SOCIAL-MEDIA\server\node_modules\express\lib\router\route.js:119:3)
at Layer.handle [as handle_request] (F:\Projects\SOCIAL-MEDIA\server\node_modules\express\lib\router\layer.js:95:5)
at F:\Projects\SOCIAL-MEDIA\server\node_modules\express\lib\router\index.js:284:15
at Function.process_params (F:\Projects\SOCIAL-MEDIA\server\node_modules\express\lib\router\index.js:346:12)
at next (F:\Projects\SOCIAL-MEDIA\server\node_modules\express\lib\router\index.js:280:10)
at Function.handle (F:\Projects\SOCIAL-MEDIA\server\node_modules\express\lib\router\index.js:175:3)
at router (F:\Projects\SOCIAL-MEDIA\server\node_modules\express\lib\router\index.js:47:12)
and My Insomnia Preview Is Error: Failure when receiving data from the peer This Is code of the MiddleWare File Named requireUser.js
module.exports =async(req,res,next)=>{
if(!req.headers || !req.headers.authorization || !req.header.authorization.startsWith('Bearer')){
return res.status(401).send("Authorization Is Required");
}
let accessToken= req.headers.authorization.split(" ")(1);
console.log(accessToken);
next();
}
I am Just Trying To console Log The Token Given In The Header or Applying Condition on Token
There is a typo in the last condition:
headershould beheaders