freeCodeCamp/guide/chinese/nodejs/http/index.md

1.5 KiB
Raw Blame History

title localeTitle
HTTP HTTP

HTTP

Node.js有一组内置模块无需进一步安装即可使用。类似地 HTTP模块包含通过超文本传输协议HTTP传输数据所需的一组功能。

HTTP模块可以创建一个HTTP服务器该服务器侦听服务器端口并将响应返回给客户端。

要包含模块,请使用require()函数和模块名称。

const http = require('http');

Node.js作为Web服务器

createServer()方法用于创建HTTP服务器。 res.writeHead()方法的第一个参数是状态代码, 200表示一切正常,第二个参数是包含响应头的对象。

const http = require('http');

 //create a server object:
 http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello World!'); //write a response to the client
  res.end(); //end the response
 }).listen(8000); //the server object listens on port 8000

 console.log("Server is listening on port no : 8000");

执行步骤:

  • 您应该在计算机中安装Node.js.
  • 创建一个_app.js_文件并粘贴上面的代码。
  • 现在打开工作目录中的控制台并执行命令node app.js
  • 打开浏览器并输入http://localhost:8000

_注意_要关闭服务器请在控制台中为Windows用户按ctrl + C

资源