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
原文:http://suyanzhu.blog.51cto.com/8050189/1894046