当前位置: 首页 > 新闻动态 > 软件编程

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

作者:用户投稿 浏览: 发布日期:2026-01-11
[导读]:这篇文章主要介绍了nodejs入门教程之创建一个简单应用的方法,涉及nodejs http模块的引用、端口监听等相关操作技巧,需要的朋友可以参考下

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

免责声明:转载请注明出处:http://www.sczxchw.cn/news/120995.html

扫一扫高效沟通

多一份参考总有益处

免费领取网站策划SEO优化策划方案

请填写下方表单,我们会尽快与您联系
感谢您的咨询,我们会尽快给您回复!