zoukankan      html  css  js  c++  java
  • 【nodejs】初识 NodeJS(二)

    上一节我们构建了一个基础的 http 服务器,我们可以接收 http 请求,但是我们得做点什么吧 – 不同的 http 请求,服务器应该有不同的响应。

    路由模块

    处理不同的 http 请求在我们的代码中是一个不同的部分,叫做路由选择

    我们要为路由提供请求的 url 和其他需要的 get 及 post 参数,随后路由根据这些数据来执行相应的代码,因此,我们需要查看 http 请求,从中提取出请求的 url 以及 get/post 参数。

    我们需要的所有数据都会包含在 request 对象中,该对象作为回调函数(onRequest)的第一个参数传递。想要解析 request 对象中的数据就需要调用 url 模块或者 querystring 模块,该两个模块都是 nodejs 的内置模块。

    var http = require('http');
    var url = require('url');
    
    function start() {
        function onRequest(request, response) {
            console.log(url.parse(request.url).pathname);
            response.writeHead({
                'Content-type': 'text/plain'
            });
            response.write('Hello node.js');
            response.end();
        }
    
        http.createServer(onRequest).listen(8888);
        console.log('server start...');
    }
    
    exports.start = start;

    在浏览器中输入 http://localhost:8888/start 通过终端我们可以看到请求的 url 路径,通过 url 路径为基准映射到不同的处理程序上。现在我们可以来写路由模块(route.js)了。

    function route(pathname) {
        console.log('route: ' + pathname);
    }
    
    exports.route = route;

    有了路由模块,那么我们把路由和服务器整合起来,首先扩展 server.js:

    var http = require('http');
    var url = require('url');
    
    function start(route) {
        function onRequest(request, response) {
            var pathname = url.parse(request.url).pathname;
    
            route(pathname);
    
            response.writeHead({
                'Content-type': 'text/plain'
            });
            response.write('Hello node.js');
            response.end();
        }
    
        http.createServer(onRequest).listen(8888);
        console.log('server start...');
    }
    
    exports.start = start;

    扩展 index.js

    var server = require('./server');
    var route = require('./route');
    
    server.start(route.route);

    在终端开启服务器 node index,可以看到终端输出的内容正是路由模块里面的内容,说明我们的 http 服务器已经在使用路由模块了,并会将请求的路径传递给路由:

    node index
    server start...
    route: /start
    route: /favicon.ico
  • 相关阅读:
    线段树刷题版
    HDU 1011 Starship Troopers 树形+背包dp
    POJ1741 Tree 树分治模板
    POJ3744 Scout YYF I 概率DP+矩阵快速幂
    【bzoj1875】【JZYZOJ1354】[SDOI2009]HH去散步 矩阵快速幂 点边转换
    BZOJ 3231: [Sdoi2008]递归数列 (JZYZOJ 1353) 矩阵快速幂
    POJ 2778 DNA Sequence AC自动机+矩阵快速幂
    POJ2222 Keywords Search AC自动机模板
    Python 命名空间
    svn下目录说明
  • 原文地址:https://www.cnblogs.com/yjzhu/p/6406533.html
Copyright © 2011-2022 走看看