求大神们解答 这是为啥啊 我是win8下的git bash
var http=require('http');
const hostname = '127.0.0.1';
const port = 3000;
var cheerio=require('cheerio');
var url="http://www.imooc.com/learn/348";
var json="";
//处理html生成json格式数据
//[{
// chaptertitle:"",
// videos:[title:"",id:'']
//}]
function filterChapters(html){
var $=cheerio.load(html);
var chapters=$('.learnchapter')
var courseData=[];
chapters.each(function(item){
var chapter=$(this);
var chapterTitle=chapter.find('.strong').text();
var videos=chapter.find('.video').children('li');
var chapterData={
chapterTitle:chapterTitle,
videos:[]
}
videos.each(function(item){
var video=$(this);
var videoTitle=video.text();
var id=video.attr('href').split('video/')[1];
chapterData.videos.push({
title:videoTitle,
id:id
})
})
courseData.push(chapterData);
})
return courseData;
}
//[{
// chaptertitle:"",
// videos:[title:"",id:'']
//}]
function printCourseData(courseData){
courseData.forEach(function(item){
var chapterTitle=item.chapterTitle;
})
}
http.get(url,function(res){
var html="";
res.on('data',function(data){
html+=data;
});
res.on('end',function(){
json=filterChapters(html);
//printCourseData(courseData);
})
}).on('error',function(){
console.log("获取课程错误")
})
var server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end(json);
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});