场景需求一:实现一台web服务器,可以加载css,js等样式。
前提概要: web服务器一般指网站服务器。是指驻留于因特网上某种类型计算机的程序,可以向浏览器等WEB客户端提供文档,也可以放置网站文件让全世界浏览,还可以放置数据文件,让全世界下载。
Node.js创建一个WEB服务器 1.可以让我们访问WEB服务器上面的网站 2.可以让我们下载WEB服务器上面的文件
该构建目录为:
这里是main.js的页面代码
var http = require('http');
var fs = require('fs');
var path = require('path');
var url = require('url');
//引入自定义模块
const common =require('../module/common')
这里是main.js的页面代码
http.createServer(function (request, response) {
//http://127.0.0.1:8081/login.html
-->/login.html(通过request.url获取)
//http://127.0.0.1:8081/index.html
-->/index.html(通过request.url获取)
//需求:根据后缀传过来的参数访问指定页面
//获取pathname地址---例如/login.html
let pathname=url.parse(request.url).pathname;
//如果pathname为/则默认指向/index.html
pathname=pathname=='/'?'/index.html':pathname
//可以获取后缀名--例如/login.html变为.html
let extname=path.extname(pathname)
//通过fs模块读取文件--->详见下一块代码
....
....
....
}).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');
下面是common.js的页面代码:
这里是common.js的页面代码
const fs = require("fs");
//方法一:用来匹配指定后缀名的content-type
exports.getMine=function(extname){
switch(extname){
case '.html':
return 'text/html';
case '.css':
return 'text/css';
case '.js':
return 'text/javascript';
default:
return 'text/html';
}
}
方法二:用来匹配指定后缀名的content-type
exports.getFileMine=function(extname){
//路径是相对于main.js
return new Promise((resolve,reject)=>{
fs.readFile('../data/mime.json',(err,data)=>{
if(err){
console.log(err)
reject(err)
return
}
let mimeObj=JSON.parse(data.toString())
resolve(mimeObj[extname])
})
})
}
方法三:用来匹配指定后缀名的content-type
exports.getFileMine=function(extname){
//同步方法拿到数据
var data=fs.readFileSync('../data/mime.json')
let mimeObj=JSON.parse(data.toString());
return mimeObj[extname]
}
--->
--->
--->
../data/mime.json中的数据是json格式的,数据如下:
{
".html":"text/html",
".css":"text/css",
".js":"text/javascript"
}
下面是main.js的页面代码
http.createServer(function (request, response) {
...代码省略...
//通过fs模块读取文件--->详见这一块代码
if(pathname!='/favicon.ico'){
fs.readFile('./static'+pathname,async (err,data)=>{
if(err){
response.writeHead(404, {'Content-Type': 'text/html;charset="utf-8"'});
response.end('这个页面不存在');
}
// let mime =common.getMine(extname)
let mime=await common.getFileMine(extname)
response.writeHead(200, {'Content-Type': ''+mime+';charset="utf-8"'});
response.end(data); //不加载css样式
})
}
}).listen(8081);
下面是完整的main.js代码
这里是main.js的页面代码
var http = require('http');
var fs = require('fs');
var path = require('path');
var url = require('url');
//引入自定义模块
const common =require('../module/common')
这里是main.js的页面代码
http.createServer(function (request, response) {
//http://127.0.0.1:8081/login.html
-->/login.html(通过request.url获取)
//http://127.0.0.1:8081/index.html
-->/index.html(通过request.url获取)
//需求:根据后缀传过来的参数访问指定页面
//获取pathname地址---例如/login.html
let pathname=url.parse(request.url).pathname;
//如果pathname为/则默认指向/index.html
pathname=pathname=='/'?'/index.html':pathname
//可以获取后缀名--例如/login.html变为.html
let extname=path.extname(pathname)
//通过fs模块读取文件--->详见下一块代码
if(pathname!='/favicon.ico'){
fs.readFile('./static'+pathname,async (err,data)=>{
if(err){
response.writeHead(404, {'Content-Type': 'text/html;charset="utf-8"'});
response.end('这个页面不存在');
}
// let mime =common.getMine(extname)
let mime=await common.getFileMine(extname)
response.writeHead(200, {'Content-Type': ''+mime+';charset="utf-8"'});
response.end(data); //不加载css样式
})
}
}).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');