新建http服务
|
|
使用function
目录结构
tp@V:/mnt/d/Node.js$ tree
.
├── hello.js
├── models
│ └── otherfuns.js
└── n2_funcall.js
n2_funcall.js
|
|
otherfuns.js
|
|
效果
使用模块
models/User.js
|
|
models/Teacher.js
1234567891011 var User = require('./User');function Teacher(id,name,age){User.apply(this,[id,name,age]);this.teach=function(res){res.write(this.name+"讲课");}}module.exports = Teacher;```JavaScript>n3_modelcall.js
var http = require(‘http’);
// var User = require(“./models/User”);
var Teacher = require(“./models/Teacher”)
http.createServer(function (request, response){
response.writeHead(200, {‘Content-Type’: ‘text/html; charset=utf-8’});
if(request.url!==”/favicon.ico”){
//funname = ‘fun3’;
teacher = new Teacher(1,’我来’,18);
// user.id =1;
// user.name=’我来’;
// user.age=18;
teacher.enter();
teacher.teach(response);
response.end(“response.end”);
}
}).listen(8000);
console.log(“Server running at http://127.0.0.1:8000/“);
n4_rout.js
1234567891011121314151617 var http = require('http');var url = require('url');var router = require('./models/route');http.createServer(function (request, response){response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});if(request.url!=="/favicon.ico"){var pathname = url.parse(request.url).pathname;pathname = pathname.replace(/\//, '');console.log(pathname);router[pathname](request, response);response.end("response.end");}}).listen(8000);console.log("Server running at http://127.0.0.1:8000/");
读文件
models/route.js
|
|
models/optfile.js
1234567891011121314151617181920 var fs = require('fs');module.exports = {readfileSync:function(path){//同步读取var data = fs.readFileSync(path, 'utf-8');console.log(data);console.log('同步方法执行完毕');},readfile:function(path, recall){//异步读取fs.readFile(path, function(err, data){if(err){console.log(err);}else{console.log(data.toString());recall(data);}});console.log('异步方法执行完毕');}}n5_readfile.js
123456789101112131415161718 var http = require('http');var url = require('url');var router = require('./models/route');//var optfile = require('./models/optfile');http.createServer(function (request, response){response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});if(request.url!=="/favicon.ico"){var pathname = url.parse(request.url).pathname;pathname = pathname.replace(/\//, '');console.log(pathname);router[pathname](request, response);}}).listen(8000);console.log("Server running at http://127.0.0.1:8000/");views/login.html
1 This is login page.views/admin.html
1 This is admin page.
写文件
models/optfile.js
1234567891011121314151617 var fs = require('fs');module.exports = {writefile:function(path, data, recall){//异步方式fs.writeFile(path, data, function (err){if (err){throw err;}console.log('It\'s Saved!');//文件被保存recall("write file success.");});},writeFileSync:function(path, data){//同步方式fs.writeFileSync(path, data);console.log("同步写文件完成");}}models/route.js
123456789101112131415161718192021222324 var optfile = require('./optfile');module.exports={login:function(req, res){function recall(data){res.write(data);res.end(" ");}optfile.readfile('./views/login.html', recall);},admin:function(req, res){function recall(data){res.write(data);res.end(" ");}optfile.readfile('./views/admin.html', recall);},writefile:function(req, res){function recall(data){res.write(data);res.end(" ");}optfile.writefile('./views/test.txt', 'write file ok1.', recall);}}n6_writefile.js
123456789101112131415161718 var http = require('http');var url = require('url');var router = require('./models/route');//var optfile = require('./models/optfile');http.createServer(function (request, response){response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});if(request.url!=="/favicon.ico"){var pathname = url.parse(request.url).pathname;pathname = pathname.replace(/\//, '');console.log(pathname);router[pathname](request, response);}}).listen(8000);console.log("Server running at http://127.0.0.1:8000/");
读取图片
models/optfile.js
仅能显示图片
12345678910111213141516 var fs = require('fs');module.exports = {readImg:function(path, res){fs.readFile(path, 'binary', function(err, file){if (err){console.log(err);return;}else{console.log('输出文件');res.write(file, 'binary');res.end();}});}}n7_readimg.js
1234567891011121314 var http = require('http');var optfile = require('./models/optfile');http.createServer(function (request, response){response.writeHead(200, {'Content-Type': 'image/jpeg'});if(request.url!=="/favicon.ico"){console.log('访问');optfile.readImg('./images/1.png', response);console.log("move on");}}).listen(8000);console.log("Server running at http://127.0.0.1:8000/");
路由改造
models/optfile.js
可显示文本和图片
12345678910111213141516171819202122232425262728293031323334353637383940 var fs = require('fs');module.exports = {writefile:function(path, data, recall){//异步方式fs.writeFile(path, data, function (err){if (err){throw err;}console.log('It\'s Saved!');//文件被保存recall("write file success.");});},writeFileSync:function(path, data){//同步方式fs.writeFileSync(path, data);console.log("同步写文件完成");},readImg:function(path, res){fs.readFile(path, 'binary', function(err, file){if (err){console.log(err);return;}else{console.log('输出文件');res.write(file, 'binary');res.end();}});},readfile:function(path, recall){//异步读取fs.readFile(path, function(err, data){if(err){console.log(err);}else{console.log(data.toString());recall(data);}});console.log('异步方法执行完毕');},}models/route.js
|
|
n8_routhtml.js
123456789101112131415 var http = require('http');var url = require('url');var router = require('./models/route');http.createServer(function (request, response){if(request.url!=="/favicon.ico"){var pathname = url.parse(request.url).pathname;pathname = pathname.replace(/\//, '');console.log(pathname);router[pathname](request, response);}}).listen(8000);console.log("Server running at http://127.0.0.1:8000/");