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

发布时间:2020-09-11 06:23:39 作者:Dason_yu
来源:脚本之家 阅读:109

本文实例讲述了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程序设计有所帮助。

推荐阅读:
  1. 一个简单的nodejs api开发小结
  2. 用Nodejs做一个简单的小爬虫

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

nodejs 入门教程

上一篇:分布式锁都有哪些实现方案?

下一篇:Pytorch 实现自定义参数层的例子

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》