Node.js - HTTP

Arquitetura

HTTP Module

/codes/nodejs/http/hello-simple/app.js
import http from 'node:http';
 
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});
 
server.listen(3000, '127.0.0.1', () => {
  console.log(`Server running at http://127.0.0.1:3000/`);
});
 
/codes/nodejs/http/hello-simple/app.js
import http from 'node:http';
 
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});
 
server.listen(3000, '127.0.0.1', () => {
  console.log(`Server running at http://127.0.0.1:3000/`);
});
 
$ node app.js
$ node app.js

HTTP Request

Visual Studio Code: REST Client

requests.http:

/codes/nodejs/http/hello-simple/requests.http
@hostname = 127.0.0.1
@port = 3000
@host = {{hostname}}:{{port}}
 
### Hello World
 
GET http://{{host}}/ HTTP/1.1
 
/codes/nodejs/http/hello-simple/requests.http
@hostname = 127.0.0.1
@port = 3000
@host = {{hostname}}:{{port}}
 
### Hello World
 
GET http://{{host}}/ HTTP/1.1
 

HTTP Response:

HTTP/1.1 200 OK
Content-Type: text/plain
Date: Thu, 03 Sep 2020 22:19:08 GMT
Connection: close
Content-Length: 11

Hello World
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Thu, 03 Sep 2020 22:19:08 GMT
Connection: close
Content-Length: 11

Hello World

Editar esta página