美文网首页
NSURLSession之nodejs搭建简单http服务器

NSURLSession之nodejs搭建简单http服务器

作者: liyoro | 来源:发表于2016-05-18 14:18 被阅读171次

    nodejs版本:v4.4.3
    开发工具:Sublime Text 3

    创建一个名为app.js的空文件,添加以下代码:

    //Lets require/import the HTTP module
    var http = require('http');
    
    //Lets define a port we want to listen to
    const PORT = 1337; 
    
    //We need a function which handles requests and send response
    function handleRequest(request, response) {
        response.end('It Works!! Path Hit: ' + request.url);
    }
    
    //Create a server
    var server = http.createServer(handleRequest);
    
    //Lets start our server
    server.listen(PORT, function() {
        //Callback triggered when server is successfully listening. Hurray!
        console.log("Server listening on: http://localhost:%s", PORT);
    });
    
    

    在终端里运行app.js,启动服务器:

    liyorodeMacBook-Pro:nodejs liyoro$ node app.js
    Server listening on: http://localhost:1337
    

    浏览器里面打开http://localhost:1337,可以看到handleRequest里面设置的文字。

    以上只是搭建了一个基本的http服务器,测试NSURLSession的GET、POST时,需要调度不同的页面(URL paths)、并且返回不同的数据进行测试,nodejs提供了Dispatcher模块来实现此需求。
    安装httpdispatcher模块:

    npm install httpdispatcher
    

    如果很慢、甚至装不上,请用以下方式安装:

    npm install httpdispatcher --registry=http://registry.npmjs.org
    

    新增模块后的完整代码如下:

    //Lets require/import the HTTP module
    var http = require('http');
    var dispatcher = require('httpdispatcher');
    
    var url = require('url');
    var util = require('util');
    
    //Lets define a port we want to listen to
    const PORT = 1337; 
    
    //We need a function which handles requests and send response
    function handleRequest(request, response) {
        // response.end('It Works!! Path Hit: ' + request.url);
        try {
            //log the request on console
            console.log(request.url);
            //Disptach
            dispatcher.dispatch(request, response);
        } catch(err) {
            console.log(err);
        }
    }
    
    //Create a server
    var server = http.createServer(handleRequest);
    
    //Lets start our server
    server.listen(PORT, function() {
        //Callback triggered when server is successfully listening. Hurray!
        console.log("Server listening on: http://localhost:%s", PORT);
    });
    
    //For all your static (js/css/images/etc.) set the directory name (relative path).
    dispatcher.setStatic('resources');
    
    //A sample GET request    
    dispatcher.onGet("/page1", function(req, res) {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        // res.end('Page One');
        res.end(util.inspect(url.parse(req.url, true).query))
        // res.end(util.inspect(url.parse(req.url, true)));
    });    
    
    //A sample POST request
    dispatcher.onPost("/post1", function(req, res) {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Got Post Data');
    });
    

    测试GET、POST请求,使用 Postman

    GET /page1 => 'Page One'
    POST /post1 => 'Got Post Data'
    
    代码下载
    参考资料

    相关文章

      网友评论

          本文标题:NSURLSession之nodejs搭建简单http服务器

          本文链接:https://www.haomeiwen.com/subject/csaalttx.html