标签:nodejs
nodejs是一个基于 Chrome V8 引擎的 JavaScript 运行环境.
nodejs和npm的关系,npm是nodejs package manage的缩写,实际上就是nodejs的包的管理,nodejs可以做轻型应用的服务器。
1.文件读取
var fs = require(‘fs‘); var content = fs.readFileSync("./path"); console.log(content);
2.文件的监控watch
var fs = require(‘fs‘); fs.watchFile(‘./path/watch.text‘,function(current,previous){ console.log(‘previous:‘+previous); console.log(‘current:‘+current); });
3.nodejs做一个服务器
var http = require(‘http‘); var fs = require(‘fs‘); var host ="127.0.0.1"; var port ="8080"; var server = http.createServer(function(request,response){ console.log("request.url:"+request.url); fs.readFile("./public"+request.url,function(error,data){ if(error){ response.writeHeader("404",{"Content-Type":"text/plain"}); response.end(‘您找的页面不存在!‘); }else{ response.writeHeader("200",{"Content-Type":"text/html"}); response.end(data); } }); }); server.listen(port,host,function(){ console.log("Listening:"+host +":"+port); });
本文出自 “hoolwall” 博客,请务必保留此出处http://hoolwall.blog.51cto.com/9164324/1764337
标签:nodejs
原文地址:http://hoolwall.blog.51cto.com/9164324/1764337