nodejs(二) 创建第一个应用

来源:互联网 发布:日志管理系统java设计 编辑:程序博客网 时间:2024/06/10 03:59

1.创建 test.js

// require 来载入 http 模块var  http  =  require('http');/** * 使用 http.createServer() 方法创建服务器,返回 一个对象 * 对象有一个叫做 listen 的方法,并使用 listen 方法绑定 8000 端口。 * 函数通过 request, response 参数来接收和响应数据。 */http.createServer(function  (request,  response)  {    response.writeHead(200,  {'Content-Type':  'text/html;  charset=utf-8'});    if(request.url!=="/favicon.ico"){  //清除第2此访问        console.log('访问');        response.write('hello,world');        response.end('hell,世界');//不写 end() 则没有http协议尾,但写了会产生两次访问   }}).listen(8000);console.log('Server  running  at  http://127.0.0.1:8000/');


2. 执行 node test.js

结果:Server  running  at  http://127.0.0.1:8000/

3. 在浏览器访问 http://127.0.0.1:8000


0 0
原创粉丝点击