源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

nodejs入门教程二:创建一个简单应用示例

  • 时间:2022-09-28 18:54 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要: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程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部