node.js第一课

1.js文件写代码,cmd输出(自己编写模块,导出)
 demo1.js文件
1
// let name = "张三"; 2 function parse(string) {//封装一个函数,分割字符串取出信息 3 let obj = {}; 4 string.split("&").forEach(function(item){ 5 let key = item.split("=")[0]; 6 let value = item.split("=")[1]; 7 obj[key] = value; 8 }) 9 return obj; 10 } 11 12 // module.exports.name = "张三"; 13 // exports.a = "lisi"; 14 module.exports = { 15 parse:parse 16 }

 另一个js文件

1 //demo2.js
2 //接受demo1.js里的数据,进行使用
3 let comm = require("./demo1.js");
4 let str = "userName=admin&passWord=123";
5 let finished = comm.parse(str);
6 console.log(finished)

如何使用:cmd输出

 

2.引用已经提供的核心模块(模块之间相互独立)

  

正式开始:http服务器建立

 1 //node 建立服务器
 2 //1.导入http模块
 3 
 4 let http = require("http");
 5 
 6 //2.使用http建立服务器
 7 
 8 http.createServer(function(request,response){
 9     //response响应头 (状态码 , 响应头对象)
10     response.writeHead(200,{"Content":"text/html"});
11     // 响应体
12     response.write("<h1>hello</h1>");
13     //结束响应过程
14     response.end();
15 }).listen(9000,"127.0.0.1",function(){
16     console.log('start')
17 })

开启服务器:

 

 在浏览器上输入端口和IP,实现读取服务器内容:

nice,node.js第一步

 

posted @ 2018-05-01 23:25  web前端煜  阅读(91)  评论(0)    收藏  举报