nodeJs

nodejs--day3笔记

2020-01-24  本文已影响0人  Grandperhaps

1. 服务器端

创建服务器

//用于创建网站服务器模块
const http = require('http');
//app对象就是网站服务器对象
const app = http.createServer();

app.on('request',(req,res) => {
    res.end('<h2>hellow user</h2>');
});
app.listen(3000);
console.log('创建成功')

访问url:localhost:3000

2. HTTP协议

微信截图_20200123172529.png

3. 获取请求地址

//用于创建网站服务器模块
const http = require('http');
//app对象就是网站服务器对象
const app = http.createServer();

app.on('request',(req,res) => {
//获取请求地址
//req.url
console.log(req.url);
if (req.url == '/index' || req.url == '/') {
    res.end('welcome to homepage');
}else if (req.url == '/list') {
    res.end('not found');
}

    //req.method
    //获取请求方式
console.log(req.method);
res.end('<h2>hellow user</h2>');

});

app.listen(3000);
console.log('创建成功')

4. 请求报文信息

//请求报文信息
console.log(req.headers);
console.log(req.headers['accept'])

5. 响应报文

res.writeHead(400);
//类型
res.writeHead(200,{
    'content-type':'text/html'
});
//中文编码
res.writeHead(200,{
    'content-type':'text/html;charset=utf8'
});
6.get请求参数
//用于处理url地址
const url = require('url');
//1.要解析的url地址
//2.将查询参数解析成对象形式
console.log(url.parse(req.url,true));


  
let { query, pathname } = url.parse(req.url,true);
console.log(query.name)
console.log(query.age)

if (pathname == '/index' || pathname == '/') {
    res.end('<h1>welcome to homepage哈哈</h1>');
}else if (pathname == '/list') {
    res.end('welcome to list page');
}else{
    res.end('not found');
}

7.post请求参数

//用于创建网站服务器模块
const http = require('http');
//app对象就是网站服务器对象
const app = http.createServer();
//处理请求参数模块
const querystring = require('querystring');

app.on('request',(req,res) => {
    //post参数是通过事件的方式接受的
    //data 当请求参数传递的时候触发data事件
    //end   当参宿和传递完成的时候触发end事件
    
    let postParams = '';
    req.on('data',parmas => {
        postParams += parmas;
    })
    req.on('end',() => {
        console.log(querystring.parse(postParams));
    });
    res.end('ok');
});


app.listen(3000);
console.log('创建成功')
微信截图_20200123204736.png

8. 路由

//1.引入http
//2.创建网站服务器
//3.为网站服务器对象添加请求事件
//4.实现路由功能
//获取客户端的请求方式
const http = require('http');
const url = require('url');
const app = http.createServer();

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') {

        }


});
app.listen(3000);
console.log('服务器启动成功');

9.静态

引入第三方模块npm install mine
mime.getType根据资源路径返回资源类型

const http = require('http');
const app = http.createServer();
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 == '/'?'/lunbo.html':pathname;
    //将用户的请求路径转换为实际的服务器硬盘路径
    let realpath = path.join(__dirname,'public' + pathname);
    console.log(realpath);

    let type = mime.getType(realpath);
    // console.log(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,{
            'centent-type':type
        })
        res.end(result)
    });

});
app.listen(3000);
console.log('服务器启动成功');
上一篇 下一篇

猜你喜欢

热点阅读