nodeJS
const http = require(‘http‘); const hostname = ‘127.0.0.1‘; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader(‘Content-Type‘, ‘text/plain‘); res.end(‘Hello World\n‘); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
可以通过控制台直接运行:
var http=require(‘http‘); http.createServer(function(request,response){ response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘}); if(request.url!="/favicon.ico"){ fun1(response); response.end(); } }).listen(8000); console.log(‘serer running at http://127.0.0.1:8000/‘); function fun1(res){ console.log(‘fun1‘); res.write("hello,this is fun1"); }
function fun2(res){ console.log(‘fun2‘); res.write(‘hello,this is fun2‘); } module.exports=fun2;
nodeExample.js修改为
var http=require(‘http‘); var otherFun=require("./function2.js"); http.createServer(function(request,response){ response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘}); if(request.url!="/favicon.ico"){ fun1(response); otherFun(response); response.end(); } }).listen(8000); console.log(‘serer running at http://127.0.0.1:8000/‘); function fun1(res){ console.log(‘fun1‘); res.write("hello,this is fun1"); }
控制台使用node命令执行nodeExample.js
module.exports={ fun2:function(res){ console.log(‘fun2‘); res.write(‘hello,this is fun2‘); }, fun3:function(res){ console.log(‘fun3‘); res.write(‘hello,this is fun3‘); } }
nodeExample.js
var http=require(‘http‘); var otherFun=require("./function2.js"); http.createServer(function(request,response){ response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘}); if(request.url!="/favicon.ico"){ otherFun.fun2(response); otherFun.fun3(response); response.end(); } }).listen(8000); console.log(‘serer running at http://127.0.0.1:8000/‘);
function User(id,name,age){ this.id=id; this.name=name; this.age=age; this.enter=function(){ console.log(this.name+‘进入图书馆‘); } } module.exports=User;
nodeExample.js
var http=require(‘http‘); var User=require("./User.js"); http.createServer(function(request,response){ response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘}); if(request.url!="/favicon.ico"){ user=new User(1,"张三",20); user.enter(); response.end(); } }).listen(8000); console.log(‘serer running at http://127.0.0.1:8000/‘);
运行,再增加Teacher.js
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;
nodeExample.js
var http=require(‘http‘); var Teacher=require("./Teacher"); http.createServer(function(request,response){ response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘}); if(request.url!="/favicon.ico"){ teacher=new Teacher(1,"张三",20); teacher.enter(); teacher.teach(response); response.end(); } }).listen(8000); console.log(‘serer running at http://127.0.0.1:8000/‘);
var http=require(‘http‘); var url=require(‘url‘); 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; console.log(pathname); response.end(); } }).listen(8000); console.log(‘serer running at http://127.0.0.1:8000/‘);
var http=require(‘http‘); var url=require(‘url‘); 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); response.end(); } }).listen(8000); console.log(‘serer running at http://127.0.0.1:8000/‘);
module.exports={ login:function(req,res){ res.write("this is login method!"); }, zhuce:function(req,res){ res.write("This is zhuce method"); } }
nodeExample.js
var http=require(‘http‘); var url=require(‘url‘); var route=require(‘./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); route[pathname](request,response); response.end(); } }).listen(8000); console.log(‘serer running at http://127.0.0.1:8000/‘);
var fs=require(‘fs‘); module.exports={ readFileSync:function(path){ var data=fs.readFileSync(path,‘utf-8‘); console.log(data); console.log("同步方法执行完毕"); } }
nodeExample.js
var http=require(‘http‘); var openFile=require(‘./openFile‘); http.createServer(function(request,response){ response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘}); if(request.url!="/favicon.ico"){ //这里的path是相对于openFile.js路径的 openFile.readFileSync(‘./login.html‘); response.end(); console.log(‘主程序执行完毕‘); } }).listen(8000); console.log(‘serer running at http://127.0.0.1:8000/‘);
var fs=require(‘fs‘); module.exports={ readFile:function(path){ fs.readFile(path,function(err,data){ if(err){ console.log(err); }else{ console.log(data.toString()); } }); console.log("异步方式执行完毕"); } }
nodeExample.js
var http=require(‘http‘); var openFile=require(‘./openFile‘); http.createServer(function(request,response){ response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘}); if(request.url!="/favicon.ico"){ openFile.readFile(‘./login.html‘); response.end(‘ok‘); console.log(‘主程序执行完毕‘); } }).listen(8000); console.log(‘serer running at http://127.0.0.1:8000/‘);
var fs=require(‘fs‘); module.exports={ readFile:function(path,res){ fs.readFile(path,function(err,data){ if(err){ console.log(err); }else{ console.log(data.toString()); res.write(data); } }); console.log("异步方式执行完毕"); } };
var http=require(‘http‘); var openFile=require(‘./openFile‘); http.createServer(function(request,response){ response.writeHead(200,{‘Content-Type‘:‘text/html;chaeset=utf-8‘}); if(request.url!="/favicon.ico"){ function recall(data){ response.write(data); response.end(‘ok‘); } openFile.readFile(‘./login.html‘,recall); console.log(‘主程序执行完毕‘); } }).listen(8000); console.log(‘serer running at http://127.0.0.1:8000/‘);
var fs=require(‘fs‘); module.exports={ readFile:function(path,recall){ fs.readFile(path,function(err,data){ if(err){ console.log(err); }else{ console.log(data.toString()); recall(data); } }); console.log("异步方式执行完毕"); } };
var http=require(‘http‘); var url=require(‘url‘); var route=require(‘./route.js‘); 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(/\//,‘‘); route[pathname](request,response); } }).listen(8000); console.log(‘server running at http://127.0.0.1:8000/‘);
var openFile=require(‘./openFile‘); module.exports={ login:function(req,res){ function recall(data){ res.write(data); res.end(); } openFile.readFile(‘./login.html‘,recall); } }
var fs=require(‘fs‘); module.exports={ readFile:function(path,recall){ fs.readFile(path,function(err,data){ if(err){ console.log(err); }else{ console.log(data.toString()); recall(data); } }); console.log("异步方式执行完毕"); } };
var fs=require(‘fs‘); module.exports={ writeFile:function(path,data,recall){ fs.writeFile(path,data,function(err){ if(err){ throw err; } console.log(‘This file is saved‘); recall(‘write file success‘); }); }, writeFileSync:function(path,data){ fs.writeFileSync(path,data); console.log(‘同步写文件成功‘); } }
var openFile=require(‘./openFile‘); var writeFile=require(‘./writeFile‘) module.exports={ login:function(req,res){ function recall(data){ res.write(data); res.end(); } openFile.readFile(‘./login.html‘,recall); }, writeFile:function(req,res){ function recall(data){ res.write(data); res.end(); } writeFile.writeFile(‘./one.txt‘,‘我的写入文件‘,recall) } }
var http=require(‘http‘); var url=require(‘url‘); var route=require(‘./route.js‘); http.createServer(function(request,response){ response.writeHead(200,{‘Content-Type‘:‘text/html;chaeset=utf-8‘}); if(request.url!="/favicon.ico"){ var pathname=url.parse(request.url).pathname; pathname=pathname.replace(/\//,‘‘); route[pathname](request,response); } }).listen(8000); console.log(‘serer running at http://127.0.0.1:8000/‘);
var http=require(‘http‘); var openFile=require(‘./openFile.js‘); http.createServer(function(request,response){ response.writeHead(200,{‘Content-Type‘:‘image/jpeg‘}); if(request.url!="/favicon.ico"){ openFile.readImg(‘./image.png‘,response); } }).listen(8000); console.log(‘serer running at http://127.0.0.1:8000/‘);
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(); } }) } };
if(request.url!="/favicon.ico"){ response.write(‘hello world‘); openFile.readImg(‘./image.png‘,response); }
var http=require(‘http‘); var url=require(‘url‘); var route=require(‘./route.js‘); http.createServer(function(request,response){ if(request.url!="/favicon.ico"){ var pathname=url.parse(request.url).pathname; pathname=pathname.replace(/\//,‘‘); route[pathname](request,response); } }).listen(8000); console.log(‘serer running at http://127.0.0.1:8000/‘); route.js var openFile=require(‘./openFile‘); module.exports={ login:function(req,res){ res.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘}); function recall(data){ res.write(data); res.end(); } openFile.readFile(‘./login.html‘,recall); }, showImg:function(req,res){ res.writeHead(200,{‘Content-Type‘:‘image/jpeg‘}); openFile.readImg(‘./image.png‘,res); } }
var fs=require(‘fs‘); module.exports={ readFile:function(path,recall){ fs.readFile(path,function(err,data){ if(err){ console.log(err); }else{ console.log(data.toString()); recall(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(); } }) } };
<html>
<head>
</head>
<body>
登录界面
<img src="./showImg">
</body>
</html>
原文:http://www.cnblogs.com/lyy-2016/p/6439559.html