从零开始学nodejs (创建http服务)

1、和用Java、C#相比,nodejs创建一个http服务真是简单了很多。

//require载入http模块
var http = require('http');
//调用createServer()方法创建服务器 http.createServer(function (request, response) { // 发送 HTTP 头部 // HTTP 状态值: 200 : OK // 内容类型: text/plain response.writeHead(200, {'Content-Type': 'text/plain'}); // 发送响应数据 "Hello World" response.end('Hello World\n'); }).listen(8888); // 终端打印如下信息 console.log('Server running at http://127.0.0.1:8888/');

 然后在node 命令执行以上的代码:

node server.js
//执行完node server.js命令后,命令工具会显示下面提示 Server running at http://127.0.0.1:8888/

 最后,我们用浏览器打开http://127.0.0.1:8888/ ,显示 "Hello World”,这就代表我们运行成功了。

参考:https://www.runoob.com/nodejs/nodejs-http-server.html

猜你喜欢

转载自www.cnblogs.com/mengxiangzhi/p/9164468.html