I'm having trouble with chapter 6, after the initial setup of the server within index.js I attempt to go to 127.0.0.1:1337/ within my browser and get the following error in my terminal;
TypeError: Cannot read property 'toUpperCase' of undefined
at Server. (/Users/LewisEccles/Documents/Git projects/human-resources/index.js:8:26)
at emitTwo (events.js:106:13)
at Server.emit (events.js:191:7)
at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:543:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:105:23)
Looking at the error its obvious its on line 8 in my code which is;
req.method = req.method.toUpperCase();
however looking at the index.js file here on github, it is an exact match, I've even copied and pasted it over my code however it still doesn't work.
Due to the error, I get an error in chrome saying that 127.0.0.1 refused to connect.
Does anyone know how I can fix this error, or provide me some guidance on where to look for an answer?
My full index.js is below in case anyone wants to see it there.
Thanks
var http = require('http');
http.createServer(function (res,req) {
//A parsed url to work with in case there are parameters
var _url;
//In case the client uses lower case for methods
req.method = req.method.toUpperCase();
console.log(req.method + ' ' + req.url);
if (req.method !== 'GET') {
res.writeHead(501, {
'Content-Type': 'text/plain'
});
return res.end(req.method + ' is not implemented by this server');
}
if (_url = /^\/employees$/i.exec(req.url)) {
//return a list of employees
res.writeHead(200);
return res.end('employee list');
} else if (_url = /^\/employees\/(\d+)$/i.exec(req.url)) {
//find employee by id in the route
res.writeHead(200);
return res.end('a single employee');
} else {
//try to send the static file
res.writeHead(200);
return res.end('static file maybe');
}
}).listen(1337, '127.0.01');
console.log('Sever Running at http://127.0.0.1:1337/');