缩写含义
- http是nodejs的服务模块
- url是url路由模块
- fs是文件服务器模块
nodejs服务器的创建:
//主机IP
const ip = '192.168.1.102';
//端口号
const port = 2000;
//引入的组建模块 http、url、fs
const http = require('http');
const url = require('url');
const fs = require('fs');
//创建一个服务
var server = http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/plain'});
res.write('my nodejs');
res.end();
});
//监听一个端口
server.listen(port,ip,function(){
console.log('server start');
});
抽象方法及获取URL部分块的内容 url
//主机IP
const ip = '192.168.1.102';
//端口号
const port = 2000;
//引入的组建模块 http、url、fs
const http = require('http');
const url = require('url');
const fs = require('fs');
//创建服务的回掉函数
var funSer = function(req,res){
//获取url地址块的内容 如:/path/show
var parth = url.parse(req.url).pathname;
res.write(parth);
res.end();
}
//监听端口的回掉
var fun = function(){
console.log('server start');
}
var server = http.createServer(funSer).listen(port,ip,fun);
读取文件的内容 File System
//主机IP
const ip = '192.168.1.102';
const port = 2000;//端口号
//引入的组建模块 http、url、fs
const http = require('http');
const url = require('url');
const fs = require('fs');
//真正打印文件内容
fs.readFile('./index.html', (err, data) => {
if (err) throw err;
//打印字符串内容
console.log(data.toString());
});
//创建服务的回掉函数
var funSer = function(req,res){
//获取url地址块的内容 如:/path/show
var parth = url.parse(req.url).pathname;
res.write(parth);
res.end();
}
//监听端口的回掉
var fun = function(){
console.log('server start');
}
var server = http.createServer(funSer).listen(port,ip,fun);
读取文件的内容并在浏览器中输出 File System
//主机IP
const ip = '192.168.1.102';
//端口号
const port = 2000;
//引入的组建模块 http、url、fs
const http = require('http');
const url = require('url');
const fs = require('fs');
//读取文件内容
var data = fs.readFileSync('./index.html');
//创建服务的回掉函数
var funSer = function(req , res){
//获取url地址块的内容 如:/path/show
var path = urls.parse( req.url ).pathname;
res.write( data.toString() );
res.write( path );
res.end();
}
//监听端口的回调
var fun = function(){
console.log('server start');
}
var server = http.createServer(funSer).listen(port,ip,fun);
完整实例(根据不同的url地址请求不同的文件【模板】)
//主机IP
const ip = '192.168.1.102';
//端口号
const port = 2000;
//引入的组建模块 http、url、fs
const http = require('http');
const url = require('url');
const fs = require('fs');
//实例化一个服务容器
var server = new http.Server();
//监听一个端口
server.listen(port , ip);
//注册一个事件处理的on方法
server.on('request' , function(req , res){
//获取请求的url地址
var url = urls.parse(req.url);
//console.log(url.pathname);
//根据path路径来读取不同的模板文件
switch( url.pathname ){
case '' || '/':
//读取文件内容
fs.readFile('./index.html',function( error, content){
if(error){//如果有错误时,显示错误信息
res.writeHead(400,{'Content-Type':'text/plain;charset="utf-8"'});
res.write(error.message);
res.end();
}else{
//正确时浏览器输出模板文件的内容
res.writeHead(200,{'Content-Type':'text/html;charset="utf-8"'});//头信息
res.write(content);//模板文件内容
res.end();
}
});
break;
case '/list':
fs.readFile('./list.html',function( error, content){
if(error){
res.writeHead(400,{'Content-Type':'text/plain;charset="utf-8"'});
res.write(error.message);
res.end();
}else{
res.writeHead(200,{'Content-Type':'text/html;charset="utf-8"'});
res.write(content);
res.end();
}
});
break;
case '/show':
fs.readFile('./show.html',function( error, content){
if(error){
res.writeHead(400,{'Content-Type':'text/plain;charset="utf-8"'});
res.write(error.message);
res.end();
}else{
res.writeHead(200,{'Content-Type':'text/html;charset="utf-8"'});
res.write(content);
res.end();
}
});
break;
default:
fs.readFile('./default.html',function( error, content){
if(error){
res.writeHead(400,{'Content-Type':'text/plain;charset="utf-8"'});
res.write(error.message);
res.end();
}else{
res.writeHead(200,{'Content-Type':'text/html;charset="utf-8"'});
res.write(content);
res.end();
}
});
break;
}
});