Node的使用
前言
- 简单的说 Node.js 就是运行在服务端的 JavaScript, 是一个基于Chrome JavaScript 运行时建立的一个平台
创建web服务器:
const http = require('http');
const app = http.createServer();
app.listen(3000);
console.log('服务器已启动,监听3000端口,请访问 localhost:3000')
监听客户端的请求
app.on('request', (req, res) => {
req.headers
req.url
req.method
console.log(res)
});
get请求
- 引入url系统模块解析请求路径,解析结果是一个包含请求host、pathname、search、query等信息的对象
const url = require('url')
app.on('request', (req,res) => {
const method = req.method
const {pathname, query} = url.parse(req.url, true)
console.log(pathname);
console.log(query);
})
post请求
- 引入querystring系统模块把body里面的字符串参数解析成包含键值对的对象
html页面发送的post请求,请求类型为表单类型
function getParams() {
window.fetch('http://localhost:3000/getnum',{
method: 'POST',
body: 'username=lisi&age=12',
headers: {
"content-type": 'x-www-form-urlencoded'
}
}).then(res => {
console.log(res);
})
}
const querystring = require('querystring');
app.on('request', (req, res) => {
let postData = '';
req.on('data', (chunk) => postData += chunk;);
req.on('end', () => {
console.log(querystring.parse(postData));
});
});
html发送post请求,请求参数类型是json对象
function getParams() {
window.fetch('http://localhost:3000/getnum',{
method: 'POST',
body: JSON.stringify({username: 'lisi',age: 12}),
headers: {
"content-type": 'application/json'
}
}).then(res => {
console.log(res);
})
}
if(req.method == 'POST') {
var parsedata = ''
req.on('data',(data)=> {
parsedata += data
})
req.on('end',()=> {
console.log(parsedata);
console.log(JSON.parse(parsedata));
})
}
设置响应报文
res.writeHead(200, {'Content-Type': 'text/html;charset=utf8‘
});
路由
app.on('request', (req, res) => {
const method = req.method.toLowerCase();
const pathname = url.parse(req.url).pathname;
res.writeHead(200, {
'content-type': 'text/html;charset=utf8'
});
if (method == 'get') {
if (pathname == '/' || pathname == '/index') {
res.end('欢迎来到首页')
}else if (pathname == '/list') {
res.end('欢迎来到列表页')
}else {
res.end('您访问的页面不存在')
}
}else if (method == 'post') {
}
});
访问静态资源
const url = require('url');
const path = require('path');
const fs = require('fs');
const mime = require('mime');
app.on('request', (req, res) => {
let pathname = url.parse(req.url).pathname;
pathname = pathname == '/' ? '/default.html' : pathname;
let realPath = path.join(__dirname, 'public' + pathname);
let type = mime.getType(realPath)
fs.readFile(realPath, (error, result) => {
if (error != null) {
res.writeHead(404, {
'content-type': 'text/html;charset=utf8'
})
res.end('文件读取失败');
return;
}
res.writeHead(200, {
'content-type': type
})
res.end(result);
});
});
同步和异步API
fs.readFile('./demo.txt', 'utf8', (err, result) => {
console.log(result);
});
let resCity = fs.readFileSync(`${__dirname}/city.json`,'utf-8')
console.log(JSON.parse(resCity));
let baseData = fs.readFileSync(`${__dirname}/images/swiper1.jpg`).toString('base64')
const myjpg = Buffer(baseData,'base64')
fs.writeFileSync(`${__dirname}/1.jpg`,myjpg)
模块
文件读取fs.readFile(文件名称,文件编码,callback)
文件写入fs.writeFile(文件名称,写入内容,callback)
const fs = require('fs');
fs.writeFile('./demo.txt', '写入的内容',err => {
if (err != null) {
console.log(err);
return;
}
console.log('写入成功');
})
const fs = require('fs');
fs.readFile('./helloworld.js', 'utf8', (err, doc) => {
console.log(doc);
console.log(err);
})
- path路径模块
path.join(__dirname,'路径')
——dirname获取当前文件的绝对路径
const path = require('path');
const a = path.join('public','require','arrtor');
console.log(a);
- querystring参数解析模块
可以把字符串拼接的url解析成键值对 - url模块
url.parse(req.url,true)可以把请求路径解析成对象
- nodemon 热更新服务器
npm install nodemon --save-dev
nodemon app.js
demo
const http = require('http')
const path = require('path')
const url = require('url')
const fs = require('fs')
const querystring = require('querystring')
const app = http.createServer()
app.on('request',(req,res) => {
console.log(req.method);
let {query,pathname} = url.parse(req.url,true)
console.log(pathname);
res.writeHead(200,{"content-type": 'text/html;charset=utf8'})
if(req.method == 'GET') {
if(pathname == '/1.json') {
let filepath = path.join(__dirname,'public','1.json')
fs.readFile(filepath,'utf8',(error,result) => {
if(error != null) {
res.writeHead(404,{"content-type": 'text/html;charset=utf8'})
res.end('page is not found')
}else {
res.writeHead(200,{"content-type": 'application/json'})
res.end(result)
}
})
}else if(pathname == '/1.html') {
let filepath = path.join(__dirname,'public','1.html')
fs.readFile(filepath,'utf8',(error,result) => {
res.writeHead(200,{"content-type": 'text/html;charset=utf8'})
if(error != null) {
res.end('page is not found')
}else {
res.end(result)
}
})
}
}else if(req.method == 'POST') {
console.log(pathname);
if(pathname == '/post') {
let postdata = ''
req.on('data',(chunk) => {
console.log(chunk.toString());
postdata += chunk
})
req.on('end',() => {
console.log(querystring.parse(postdata));
})
}
}
})
app.listen(3000,() => {
console.log("server in localhost: 3000,服务器启动成功!");
})