区别请求内容

126 阅读1分钟

区别请求内容

const http = require('http')
//引入http模块
//创建web服务 
const server = http.createServer(function (request, response) { 
//request 请求 
//response 响应 
//区分请求 
console.log(request.url);
let path = request.url 
//请求url路径
response.writeHead(200, { 'content-type': 'text/html;charset=utf-8' })
//解决中文乱码 
if (path == '/login') { let loginStr = 
                                `<div>
                                 <form>
                            <input type="text" name="username"/></br>
                            <input type="password" name="password"/>  </br> 
                            <input type="submit" value="登录"/></br> 
                                </form> 
                                </div> ` 
                    response.write(loginStr)
                    } else if (path == '/productlist') {
                    let productList = [{ 
                    id: 1001,
                    name: 'javascript高级编程',
                    price: 45.89,
                    url: './image/shoppingBg_03.jpg'
                    },
                    { 
                    id: 1002,
                    name: 'css高级编程',
                    price: 35.89,
                    url: './image/shoppingBg_06.jpg'
                    },
                    {
                    id: 1003,
                    name: 'html高级编程',
                    price: 25.89, 
                    url: './image/shoppingBg_06.jpg'
                    }, 
                    { 
                    id: 1004, 
                    name: 'vue高级编程', 
                    price: 65.89,
                    url: './image/shoppingBg_06.jpg'
                    } 
                    ]
                    response.write(JSON.stringify(productList))
                    } else {
                    response.write('此模块开发中...')
                    //写入内容到响应对象response 
                    }
                    response.end() 
                    //结束写入,响应内容 
                    })
server.listen(3000, () => console.log('web服务启动成功,监听 3000端...'));
//启动web服务