您的位置:首页 > Web前端 > Node.js

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

2017-04-24 10:16 766 查看

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 入门教程
相关文章推荐