NODEJS初识【写一个简单路由】
程序员文章站
2022-06-03 20:19:46
...
//导入模块
const http = require("http");
const url = require("url");
const fs = require("fs");
const route = require("./router")
//创建服务器
var server = http.createServer(function(req,res){
//访问服务器
//url解析
var pathname = url.parse(req.url,true).pathname;
console.log(pathname + "has received");
//路由:pathname == "/"
//加载这个页面,并渲染到浏览器,目前只能渲染一个文件,正在想加载多个文件
if (pathname == "/") {
fs.readFile("./index.html", function(err, data) {
res.writeHead(200, {
"Content-Type": "text/html"
})
res.write(data)
console.log("html fininshed")
// res.end()
})
}
if (pathname == "/bbb") {
fs.readFile("./css.css", function(err, data) {
res.writeHead(200, {
"Content-Type": "text/css"
})
res.write(data)
console.log("css fininshed")
res.end()
})
}
if (pathname == "/ccc") {
fs.readFile("./css.css", function(err, data) {
res.writeHead(200, {
"Content-Type": "text/css"
})
res.write(data)
console.log("css fininshed")
res.end()
})
}
})
server.listen("8081","192.168.51.222")
console.log("server running");
上一篇: 她一生坎坷,好不容易当上了皇后却被废了