第七节 创建web服务器

一.使用http创建web服务器

1.功能

1).接受HTTP请求(GET,POST,DELETE,PUT等等)
2).处理http请求

2.创建的web服务器架构

1).Nginx/Apache:负责接受http请求,确定谁来处理请求,并返回请求的结果
2).php-fpm/php模块
3).常见的请求:
①请求文件:包括要处理的静态文件(网页,图片,css文件,js文件)
②完成特定的操作,如登录,获取特定数据等等
4).node的web服务器
①不依赖其他特定的服务器软件
②node代码负责web服务器的各种配置

//导入node的核心模块http
var http = require('http');
//请求的回调函数
var reqHandler = function(req,res){
        res.writeHead(200,{'Content-Type':'text/html'});
        res.write("<head><meta charset='utf-8'></head>");
        res.end("aaaaaaa");
}
//创建http的实例
var web = http.createServer(reqHandler);
web.listen(8080);
console.log('http running on http://localhost:8080');

5).使用http创建web服务器
我们将handle对象(index.js)作为参数传给服务器(server.js),再由路由(router.js)接收,最后由路由来判断当前路径对应的请求处理程序(requestHandler.js)存在否, 存在的话就调用对应的函数(index.js)。

//单入口文件index.js
var server = require('./server');
var router = require('./router');
var requestHandlers = require('./requestHandlers');
//定义关联数组对象
var handle = {};
//声明触发相对应的事件处理程序
handle['/']=requestHandlers.start;
handle['/start']=requestHandlers.start;
handle['/upload']=requestHandlers.upload;
server.start(router.route,handle);
//路由文件router.js
function route(handle,pathname){
    console.log('About to route a request for '+pathname);
    if(typeof handle[pathname]==='function'){
    return handle[pathname]();
    }else{
        console.log('No request handler found for'+pathname);
        return "404 Not found";
    }
}
exports.route = route;//用于外部文件使用这个方法
//服务脚本server.js
var http = require('http');//引入http模块
var url = require('url'); //引入url模块
function start(route,handle){
    function onRequest(req,res){
        var pathname = url.parse(req.url).pathname;//获取用户输入的url
        var content = route(handle,pathname);
        //调用route函数,赋值给content
        console.log('Request for '+pathname+' Received.');
        res.writeHead(200,{'Content-Type':'text/plain'});
        //发送一个HTTP状态200和HTTP头的内容类型
        res.write(content);//发送文本content
        res.end();//完成响应过程
    };
http.createServer(onRequest).listen(8888);
//创建服务并监听端口
console.log('Server has started');
}
exports.start = start;
//用于外部文件使用这个方法
//事件处理requestHandler.js
function start(){
    console.log('你访问的是 /start');
    return "hello start";
}
function upload(){
    console.log('你访问的是 /upload');
    return "hello upload";
}
exports.start = start;//用于外部文件使用这个方法
exports.upload = upload;//用于外部文件使用这个方法

二.使用Express创建web服务器

1.创建Express项目

1).安装express
npm install -g express
npm install -g express-generator
2).创建项目
express JadeWebApp //默认是jade引擎
express -e EjsWebApp //要创建ejs引擎用下面的命令
3).安装项目
cd 项目目录;
npm install
4).开启服务
npm start
node ./bin/www
这里写图片描述

2.静态文件

1).静态文件范围:网页,纯文本,图片,javascript,css样式表,媒体文件,字体文件.
2).使用Express创建静态文件服务

3.路由

将不同的请求,分配给响应的处理函数
三种路由的方法:
1)path方法
2).Router方式
3).route

//引入express模块
var express = require('C:/Users/admin/AppData/Roaming/npm/node_modules/express');
//创建express实例
var app = express();
app.use(express.static('./public'));
//路由
//Router方式
var Router = express.Router();
Router.get('/add',function(req,res){
        res.end('this is a Router add test');
});
Router.get('/list',function(req,res){
        res.end('this is a Router list test');
});
app.use('/post',Router);
//Path方式
app.get('/',function(req,res){
        res.end('mingming love');
});

//基础路由
app.route('/content')
        .get(function(req,res){
                res.end('route content get');
        })
        .post(function(req,res){
                res.end('route content post');
        });
app.param('id',function(req,res,next,id){
        req.id = id;
        next();
});
app.get('/content/:id',function(req,res){
        res.end('content id :'+req.id);
});
app.listen(8080,function afterListen(){
        console.log('express running on http://localhost:8080');
});

4.中间件

三.创建TCP服务器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值