NSURLSession之nodejs搭建简单http服务器

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'
代码下载
参考资料
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,001评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,638评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • Nginx1.8.0的应用场景 http服务器(图片服务器/静态网页服务器)。Nginx是一个可以独立提供http...
    刘计计计阅读 145评论 0 0
  • 非常喜欢这章节的一句话:“你拥有这么神奇的大脑、这么令人敬畏的智力,那么对于学习你还有什么可害怕和担忧的呢?”以后...
    蒙奇奇尼尼阅读 285评论 2 2