单页面的部署
摘要:1、我也不知道是不是叫单页面部署,就是把你做的html页面,监听起来,然后在浏览器上面输入地址就能访问了;
2、其实跟我们昨天写的一样,就是把html文件读取出来,然后在写到浏览器页面上response.end(data)/response.write(data);response.end()
- 访问index.html
- 在浏览器中输入“127.0.0.1:8000”就能得到index.html的页面了;
目录结构:
-----public---index.html
-----http.js
//======================
const http = require("http");
const fs = require("fs");
http.createServer(function(req,res){
fs.readFile("public/index.html", function(err,data){
res.writeHead(200,{"Content-Type":"text/html"});
res.write(data);
res.end();
})
}).listen(8000,"127.0.0.1")
2、访问index.html中的css/javascript文件;
——-1、如果直接在index.html中链接css/js文件,还是按照什么的写法,是访问不到css/js文件的;
——-2、按照上面的思路,这两个文件(css、js),还是可以用“fs”模块读取然后写到响应中去write();
目录结构:
-----public---index.html
---index.css
---index.js
-----http.js
//demo=================================
const http = require("http");
const fs = require("fs");
const url = require("url");
http.createServer(function(req,res){
var pathname = url.parse(req.url).pathname;
var ext = pathname.match(/(\.[^.]+|)$/)[0];
switch (ext){
case ".css" :
case ".js" :
fs.readFile("./public/"+req.url, function(err,data){
res.writeHead(200,{"Content-Type":{
".css" : "text/css",
".js" : "application/javascript"
}[ext]
});
res.write(data);
res.end();
})
break;
default :
fs.readFile("public/index.html", function(err,data){
res.writeHead(200,{"Content-Type":"text/html"});
res.write(data);
res.end();
})
}
}).listen(8000,"127.0.0.1")