nodejs靜態(tài)資源服務(wù)器
1. touch 一般是在創(chuàng)建一個空的日志文件的時候才會使用
2. xshell 使用open命令來打開一個文件
3. http是nodejs的服務(wù)模塊
4. url是url路由模塊
5. fs是文件服務(wù)器模塊
nodejs實(shí)例
打開xshell 打開ubuntu 用xshell鏈接ubuntu
1. nodejs服務(wù)器的創(chuàng)建:
const http=require('http');
const ip = '本機(jī)IP地址';
const port = 3000;
http.createServer((req,res)=>{
res.writeHead(200,{'Content-type':'text/html'});
res.write('hello');
res.end();
}).listen(port,ip,()=>{
console.log('server start');
});
//主機(jī)IP
const ip = '192.168.0.110';
//端口號
const port = 3000;
//引入的組建模塊 http懂牧、url山上、fs
const http = require('http');
const url = require('url');
const fs = require('fs');
//創(chuàng)建一個服務(wù)
var server = http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/plain'});
res.write('my nodejs');
res.end();
});
//監(jiān)聽一個端口
server.listen(port,ip,function(){
console.log('server start');
});
//封裝成函數(shù)調(diào)用實(shí)例
const http=require('http');
const ip = '本機(jī)IP地址';
const port = 3000;
var f = function(req,res){
res.writeHead(200,{'Content-type':'text/html'});
res.write('hello');
res.end();
}
http.createServer(f).listen(port,ip,()=>{
console.log('server start');
});
2. 抽象方法及獲取URL部分塊的內(nèi)容 url
const ip = '192.168.0.110';//主機(jī)IP
const port = 3000;//端口號
//引入的組建模塊 http娶眷、url、fs
const http = require('http');
const url = require('url');
const fs = require('fs');
//創(chuàng)建服務(wù)的回掉函數(shù)
var funSer = function(req,res){
//獲取url地址塊的內(nèi)容 如:/path/show
var parth = url.parse(req.url).pathname;
res.write(parth);
res.end();
}
//監(jiān)聽端口的回掉
var fun = function(){
console.log('server start');
}
var server = http.createServer(funSer).listen(port,ip,fun);
const http=require('http');
const url = require('url');
const ip = '本機(jī)IP地址';
const port = 3000;
var f = function(req,res){
var pathname = url.parse(req.url).pathname;
res.write(pathname);
res.end();
}
var f2 = function(){
console.log('server start');
}
http.createServer(f).listen(port,ip,f2);
3. 讀取文件的內(nèi)容 File System
const http=require('http');
const fs = require('fs');
const url = require('url');
const ip = '本機(jī)IP地址';
const port = 3000;
var pathname = url.parse(req.url).pathname;
var userurl = url.parse(pathname);
switch(userurl){
case '' || '/':
fs.readFile('./index.html',func(err,content){
if(err){
console.log(err);
}
else{
res.writeHead(200,{'Content-type':'text/html'});
}
});
}
const ip = '192.168.0.110';//主機(jī)IP
const port = 3000;//端口號
//引入的組建模塊 http或渤、url、fs
const http = require('http');
const url = require('url');
const fs = require('fs');
//真正打印文件內(nèi)容
fs.readFile('./index.html', (err, data) => {
if (err) throw err;
//打印字符串內(nèi)容
console.log(data.toString());
});
//創(chuàng)建服務(wù)的回掉函數(shù)
var funSer = function(req,res){
//獲取url地址塊的內(nèi)容 如:/path/show
var parth = url.parse(req.url).pathname;
res.write(parth);
res.end();
}
//監(jiān)聽端口的回掉
var fun = function(){
console.log('server start');
}
var server = http.createServer(funSer).listen(port,ip,fun);
4. 讀取文件的內(nèi)容并在瀏覽器中輸出 File System
const ip = '192.168.0.110';//主機(jī)IP
const port = 3000;//端口號
//引入的組建模塊 http、url涌矢、fs
const http = require('http');
const url = require('url');
const fs = require('fs');
//讀取文件內(nèi)容
var data = fs.readFileSync('./index.html');
//創(chuàng)建服務(wù)的回掉函數(shù)
var funSer = function(req , res){
//獲取url地址塊的內(nèi)容 如:/path/show
var path = urls.parse( req.url ).pathname;
res.write( data.toString() );
res.write( path );
res.end();
}
//監(jiān)聽端口的回掉
var fun = function(){
console.log('server start');
}
var server = http.createServer(funSer).listen(port,ip,fun);
5. 完整實(shí)例(根據(jù)不同的url地址請求不同的文件【模板】)
const ip = '192.168.0.110';//主機(jī)IP
const port = 3000;//端口號
//引入的組建模塊 http摆出、url朗徊、fs
const http = require('http');
const url = require('url');
const fs = require('fs');
//實(shí)例化一個服務(wù)容器
var server = new http.Server();
//監(jiān)聽一個端口
server.listen(port , ip);
//注冊一個事件處理的on方法
server.on('request' , function(request , response){
//解析請求的url
var url = urlapi.parse(request.url);
//監(jiān)聽請求的網(wǎng)站,以當(dāng)前腳本目錄為根目錄的url地址
console.log(url.pathname);
//特殊URL會讓服務(wù)器在發(fā)送響應(yīng)前先等待
//根據(jù)path路徑來讀取不同的模板文件
switch( url.pathname ){ //判斷請求的路徑信息
case '' || '/': //處理請求網(wǎng)站根目錄偎漫,指定加載對應(yīng)的文件夾爷恳,一般以根目錄的index.html為默認(rèn),nodejs是高效流處理的方案象踊,也可以通過配置文件來設(shè)置
//讀取文件內(nèi)容(打開請求的文件)
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{
//正確時瀏覽器輸出模板文件的內(nèi)容
res.writeHead(200,{'Content-Type':'text/html;charset="utf-8"'});//告訴相應(yīng)的請求頭信息,返回?cái)?shù)據(jù)
res.write(content);//模板文件內(nèi)容
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;
}
});
練習(xí):http 文件(每次都需要重啟服務(wù)器)
const http=require('http');
const ip = '本機(jī)IP地址';
const port = 3000;
http.createServer((req,res)=>{
res.writeHead(200,{'Content-type':'text/html'});
res.write('hello');
res.end();
}).listen(port,ip,()=>{
console.log('server start');
});
const http=require('http');
const ip = '本機(jī)IP地址';
const port = 3000;
var f = function(req,res){
res.writeHead(200,{'Content-type':'text/html'});
res.write('hello');
res.end();
}
http.createServer(f).listen(port,ip,()=>{
console.log('server start');
});
const http=require('http');
const ip = '本機(jī)IP地址';
const port = 3000;
var f = function(req,res){
res.writeHead(200,{'Content-type':'text/html'});
res.write('hello');
res.end();
}
var f2 = function(){
console.log('server start');
}
http.createServer(f).listen(port,ip,f2);
const http=require('http');
const url = require('url');
const ip = '本機(jī)IP地址';
const port = 3000;
var f = function(req,res){
var pathname = url.parse(req.url).pathname;
res.write(pathname);
res.end();
}
var f2 = function(){
console.log('server start');
}
http.createServer(f).listen(port,ip,f2);
const http=require('http');
const fs = require('fs');
const url = require('url');
const ip = '本機(jī)IP地址';
const port = 3000;
<!-- rs.readFile('xiaoniu.txt',(err,data)=>{
if(err) throw err;
console.log(data.toString());
}); -->
var data = fs.readFileSync('xiaoniu.txt');
var f = function(req,res){
var pathname = url.parse(req.url).pathname;
res.write(pathname);
res.end();
}
var f2 = function(){
console.log('server start');
}
http.createServer(f).listen(port,ip,f2);
const http=require('http');
const fs = require('fs');
const url = require('url');
const ip = '本機(jī)IP地址';
const port = 3000;
var pathname = url.parse(req.url).pathname;
var userurl = url.parse(pathname);
switch(userurl){
case '' || '/':
fs.readFile('./index.html',func(err,content){
if(err){
console.log(err);
}
else{
res.writeHead(200,{'Content-type':'text/html'});
}
});
}