我是靠谱客的博主 酷酷果汁,这篇文章主要介绍nodejs入门教程二:创建一个简单应用示例,现在分享给大家,希望可以做个参考。

本文实例讲述了nodejs创建一个简单应用的方法。分享给大家供大家参考,具体如下:

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

希望本文所述对大家nodejs程序设计有所帮助。

最后

以上就是酷酷果汁最近收集整理的关于nodejs入门教程二:创建一个简单应用示例的全部内容,更多相关nodejs入门教程二内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(200)

评论列表共有 0 条评论

立即
投稿
返回
顶部