spbooks / mean1 Goto Github PK
View Code? Open in Web Editor NEWCode archive for the book Full Stack JavaScript Development with MEAN
Code archive for the book Full Stack JavaScript Development with MEAN
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/');
I ran this and when I checked the employees data using mongodb the team attribute was null;
To fix this I needed change 2 functions:
if (error) {
console.error('Error: ' + error);
} else {
data.team_id = team1._id;
}
changed to
if (error) {
console.error('Error: ' + error);
} else {
team1.forEach(function (team1a) {
data.teams.forEach(function (team) {
if (team.name === team1a.name) {
team.team_id = team1a._id;
}
});
});
}
Employee.update({}, {
team: data.team_id
}, {
changed to
Employee.update({}, {
team: team.team_id
}, {
You should replace the last lines of index.js
...
} else {
// Try to send static files
res.writeHead(200);
res.end('static file maybe');
}
with
...
} else {
staticFile(req.url,res);
}
The program as is will continue to respond with 'static file maybe'
to every request not intercepted by the preceding routing tests.
In both insertTeams and insertEmployees :
When the array is passed to create for either Team or Employee, later on, both devops and acct are undefined and pd is an array.
I had to remove the square brackets when create is called for both Team and Employee for later code to work correctly.
In Listing 9-9 the Models Employee and Team are not Imported. I added
require('../models/employee');
require('../models/team');
to make it work.
After substituting the script as stated, the http://localhost/employees hangs forever.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.