首页 > Web开发 > 详细

Nodejs 的http服务器编写成nodejs的模块

时间:2017-01-24 16:18:28      阅读:197      评论:0      收藏:0      [点我收藏+]

1.创建server.js文件

2.编辑server.js文件

    server.js文件内容如下:

    var http = require("http");

    

    function start(){

     function onRequest(request,response){

     response.writeHead(200,{"Content-Type":"text/plain"});

     response.write("Hello world");

     response.end();

     }

    

     http.createServer(onRequest).listen(8888);

     console.log("Server has started");

    }

    

    exports.start = start;

3.使用server.js这个模块

    a.创建index.js文件

    b.编辑index.js文件

        编辑内容如下:

        var http = require("./server");

        http.start();

    c.执行index.js文件

        node index.js

    d.查看执行结果

        控制台输出如下:

            Server has started

        访问http://localhost:8888/页面输出如下:

              Hello world



本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1894046

Nodejs 的http服务器编写成nodejs的模块

原文:http://suyanzhu.blog.51cto.com/8050189/1894046

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!