Node reported an error.

  node.js, question

My code is as follows:

var http = require('http');
 var url = require('url');
 var items = [];
 var server = http.createServer(function(req,res){
 switch(req.method){
 case 'POST':
 var item = '';
 req.setEncoding('utf-8');
 req.on('data',function(chunk){
 Itemplus = chunk;
 });
 req.on('end',function(){
 items.push(item);
 res.end('OK\n');
 });
 break;
 case 'GET':
 items.forEach(function(item,i){
 Res.write(i plus')' plus item plus' \n');
 });
 res.end("OK");
 break;
 case 'DELETE':
 var path = url.parse(req.url).parthname;
 var i = parseInt(path.slice(1),10);
 if (isNaN(i)) {
 res.statusCode = 400;
 res.end('invalid item in');
 }else if (!  items[i]){
 res.statusCode = 404;
 res.end('item not found');
 }else{
 items.splice(i,1);
 res.end('OK\n');
 bracket
 break;
 bracket
 });
 server.listen(3000);

In the DELETE branch, the program reported an error, prompting Cannot Call Method’ slice’ of undefined
图片描述
Slice function should be javascript’s own function, node should recognize it!

Solution:
1.cannot call method 'slice' of undefinedAs already explained, it was in aundefinedCalled on the value ofslice(). Explain yourpath=undefined
2. If the novice cannot understand the meaning of this mistake,console.log(path), can also solve the problem.

Answer:
parthnameThe attribute name is incorrectly written and should bepathname

PS: Should learn to debug more and correctly understand the error prompt. During debugging, you can also improve your understanding of the code. The paper is easy to read at last: once you debug and solve the problem yourself, you will always get unexpected things.