封装好以后通过一句话就可以创建web服务router.static(re,res,'html所在目录(静态资目录)')
代码(创建web服务)const http = require('http') const routes = require('./packaging-web-server/routes') http.createServer(function (req, res) { // routes.static(req,res,'静态web目录(就是放html文件的目录)'); routes.static(req, res, 'pack'); }).listen(3000) console.log('http://127.0.0.1:3000') 封装的web服务,路径./packaging-web-server/routesconst fs = require('fs') const path = require('path') const url = require('url') let getMimes = function (a) { var data = fs.readFileSync('../data/Mime.json');//同步方法 let site = JSON.parse(data.toString()) return site[a] } exports.static = function (req, res, staticPath) { // 1.获取网站地址 // let pathanme = req.url let pathanme = url.parse(req.url).pathname pathanme = pathanme == '/' ? '/index.html' : pathanme //path内置模块中有一个path.extname()方法获取后缀名 let extanme = path.extname(pathanme) // console.log(extanme) //2. 通过fs模块读取文件 if (pathanme != '/favicon.ico') { fs.readFile('./' + staticPath + pathanme, (err, data) => { if (err) { res.writeHead(404, { "Conten-type": 'text/html;charset="utf-8"' }) res.write('<head><meta charset="UTF-8"></head>') res.end('页面不存在') } else { let mimes = getMimes(extanme) res.writeHead(200, { "Conten-type": '' + mimes + ";charset='utf-8'" }) res.end(data) } }) } }路由:根据不同个url请求,处理不同的业务逻辑 就是http://127.0.0.1:3000/login处理登录的业务逻辑,htttp://127.0.0.1:3000/register处理注册逻辑
代码const http = require('http') const url = require('url') const routes = require('./packaging-web-server/routes') http.createServer(function (req, res) { //创建静态web服务 // routes.static(req,res,'静态web目录(就是放html文件的目录)'); routes.static(req, res, 'pack'); //路由 let pathname = url.parse(req.url).pathname switch (pathname) { case '/login': res.writeHead(200,{"Content-type":'text/html;charset="UTF-8"'}) res.end('执行登录') break; case '/register': res.writeHead(200,{"Content-type":'text/html;charset="UTF-8"'}) res.end('执行注册') break; case '/admin': res.writeHead(200,{"Content-type":'text/html;charset="UTF-8"'}) res.end('处理后的业务逻辑') break; default: res.writeHead(404,{"Content-type":'text/html;charset="UTF-8"'}) res.end('页面不存在') break; } }).listen(3000) console.log('http://127.0.0.1:3000') 自定义封装web服务模块./packaging-web-server/routesconst fs = require('fs') const path = require('path') const url = require('url') let getMimes = function (a) { var data = fs.readFileSync('./data/Mime.json');//同步方法 let site = JSON.parse(data.toString()) return site[a] } exports.static = function (req, res, staticPath) { // 1.获取网站地址 // let pathanme = req.url let pathanme = url.parse(req.url).pathname pathanme = pathanme == '/' ? '/index.html' : pathanme //path内置模块中有一个path.extname()方法获取后缀名 let extanme = path.extname(pathanme) // console.log(extanme) //2. 通过fs模块读取文件 if (pathanme != '/favicon.ico') { //try就是先执行一次 try { let data = fs.readFileSync('./' + staticPath + pathanme); if (data) { let mimes = getMimes(extanme); res.writeHead(200, { "Conten-type": '' + mimes + ";charset='utf-8'" }) res.end(data) } } catch (err) { } } }客户端和服务器之间进行请求,响应时最常见的两种方法:GET和POST
GET
一般用于获取数据 获取请求类型// 获取请求类型 console.log(req.method) //获取get传入参数//http://127.0.0.1:3000/nwes?page=2&id=1 //获取get传入参数?page=2&id=1 //传入一个true返回一个对象{ page: '2', id: '1' } //不传true返回page=2&id=1 let query = url.parse(req.url,true).query // console.log(query.page) //2 // console.log(query) //{ page: '2', id: '1' }POST
一般用于提交数据获取post传入参数 //获取post传值 let postData = ''; req.on('data',(chunk)=>{ postData = postData+chunk }) req.once('end',()=>{ console.log(postData) res.end(postData) })