”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > HTTP 服务器:Node.js

HTTP 服务器:Node.js

发布于2024-11-01
浏览:884

HTTP Server: Node.js

Introduction to Node.js HTTP Library

Node.js's http module is one of the core modules that allows developers to create HTTP servers and clients easily. It is built into Node.js, meaning no additional installation is required to use it. This module is particularly useful for creating web servers, handling HTTP requests, and sending HTTP responses.

The http module provides a straightforward interface for building network-based applications like RESTful APIs or web servers.

Basic Features of the HTTP Module

  1. Creating HTTP Servers: The http module allows you to create web servers that can listen for HTTP requests from clients and send back responses.
  2. Making HTTP Requests: You can also use the http module to make HTTP requests (client-side), sending data to remote servers.
  3. Handling Request/Response: It provides APIs to handle incoming requests (with methods, headers, and bodies) and send back responses.

Basic Example: Creating an HTTP Server

Here's a basic example demonstrating how to create an HTTP server using the http module in Node.js:

// Load the http module
const http = require('http');

// Create a server that responds to incoming requests
const server = http.createServer((req, res) => {
  // Set the HTTP status code and content type
  res.statusCode = 200; // OK
  res.setHeader('Content-Type', 'text/plain');

  // Send a response to the client
  res.end('Hello, World!');
});

// Define the port and hostname
const hostname = '127.0.0.1';
const port = 3000;

// Start the server
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Breakdown of the code:

  1. http.createServer(): This method is used to create the server. The callback function inside receives two arguments:

    • req (request): Represents the incoming HTTP request.
    • res (response): Represents the outgoing HTTP response.
  2. res.statusCode = 200: This sets the HTTP status code of the response to 200, which means "OK".

  3. res.setHeader(): Sets the HTTP headers. In this case, we set the Content-Type to text/plain, indicating the response body is plain text.

  4. res.end(): Ends the response and sends the string "Hello, World!" to the client.

  5. server.listen(): The server listens for requests on a specific port and hostname. Here, it listens on localhost (127.0.0.1) on port 3000.

Running the Code:

  1. Save the code in a file, say server.js.
  2. Run the file in your terminal with:
   node server.js
  1. Open a browser and visit http://127.0.0.1:3000/. You should see the text "Hello, World!" displayed.

req Object in Node.js

The req (request) object in Node.js represents the incoming HTTP request and is an instance of the http.IncomingMessage class. It provides several properties and methods to access details about the incoming request, such as headers, status codes, query parameters, and the body.

Key Properties & Methods of req

1. req.headers

This property contains the headers sent by the client. It's an object where the keys are the header names and the values are their corresponding values.

Code:

console.log(req.headers);

Example Output:

{ host: 'localhost:3000', connection: 'keep-alive', ... }

2. req.statusCode

This property holds the HTTP status code for the request, though it's typically used for response. It can be set to inform the server about the status.

Code:

console.log(req.statusCode); // 200 (when server responds)

Example Output:

200

3. req.statusMessage

This is the status message associated with the status code. It provides a short description of the status code.

Code:

console.log(req.statusMessage); // OK (if statusCode is 200)

Example Output:

OK

4. req.url

This property contains the full URL path of the request, including the query string.

Code:

console.log(req.url); // '/search?query=nodejs'

Example Output:

/search?query=nodejs

5. Reading Query Parameters

Query parameters are parsed from the URL. To get them, you need to manually parse the URL or use a library like url or querystring.

Code:

const url = new URL(req.url, `http://${req.headers.host}`);
console.log(url.searchParams.get('query')); // 'nodejs'

Example Output:

nodejs

6. Reading the Body (using Buffer for POST requests)

For HTTP requests like POST, the body of the request can contain data (such as form data or JSON). To read the body, you need to listen to the 'data' event and accumulate the chunks. The Buffer class can be used to handle binary data.

Code:

let body = [];  // Initialize an empty array to accumulate chunks
req.on('data', chunk => {
  body.push(chunk);  // Append each chunk to the body array
});

req.on('end', () => {
  // Concatenate all the chunks and convert the Buffer to a string
  const bodyString = Buffer.concat(body).toString();
  console.log('Body:', bodyString);  // 'name=John&age=30'
});

Example Output (when sending data like name=John&age=30 in the body):

Body: name=John&age=30

Full Example Code for req Object

Here's the full code that demonstrates how to use the req object in a Node.js HTTP server. This code handles the headers, status, URL, query parameters, and body using buffers.

const http = require('http');

const server = http.createServer((req, res) => {
  // 1. Log headers
  console.log('Headers:', req.headers);

  // 2. Log status code (normally used for response, but shown here for reference)
  console.log('Status Code:', req.statusCode);

  // 3. Log status message (again, for response handling)
  console.log('Status Message:', req.statusMessage);

  // 4. Log requested URL
  console.log('URL:', req.url);

  // 5. Read query parameters
  const url = new URL(req.url, `http://${req.headers.host}`);
  console.log('Query Parameter (query):', url.searchParams.get('query'));

  // 6. Read the body (for POST requests)
  let body = [];  // Initialize an empty array to accumulate chunks
  req.on('data', chunk => {
    body.push(chunk);  // Append each chunk to the body array
  });
  req.on('end', () => {
    // Concatenate all the chunks and convert the Buffer to a string
    const bodyString = Buffer.concat(body).toString();
    console.log('Body:', bodyString);
    res.end('Request processed!');
  });

  // Respond to the client
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
});

server.listen(3000, () => {
  console.log('Server running at http://127.0.0.1:3000/');
});

Full Output Example

When the server is running, and you visit http://127.0.0.1:3000/search?query=nodejs, the output in your terminal would look something like this:

Headers: { host: 'localhost:3000', connection: 'keep-alive', ... }
Status Code: 200
Status Message: OK
URL: /search?query=nodejs
Query Parameter (query): nodejs
Body:

If you send a POST request (e.g., using Postman with body data name=John&age=30), the output would be:

Headers: { host: 'localhost:3000', connection: 'keep-alive', ... }
Status Code: 200
Status Message: OK
URL: /search?query=nodejs
Query Parameter (query): nodejs
Body: name=John&age=30

res Object in Node.js

The res (response) object in Node.js represents the outgoing HTTP response. It is an instance of the http.ServerResponse class and provides methods to set headers, status codes, and send data back to the client.

Key Properties & Methods of res

1. res.statusCode

This property is used to get or set the HTTP status code of the response. By default, it is set to 200, indicating a successful request.

Code:

res.statusCode = 404;  // Set status code to 404 (Not Found)
console.log(res.statusCode); // 404

Example Output:

404

2. res.statusMessage

This is the status message that corresponds to the status code. It is typically a short description of the HTTP status.

Code:

res.statusMessage = 'Not Found';  // Set the status message
console.log(res.statusMessage);  // 'Not Found'

Example Output:

Not Found

3. res.setHeader(name, value)

This method is used to set a specific header in the response. You provide the header name and its corresponding value.

Code:

res.setHeader('Content-Type', 'text/html');
console.log(res.getHeader('Content-Type'));  // 'text/html'

Example Output:

text/html

4. res.getHeader(name)

This method retrieves the value of a specified header.

Code:

res.setHeader('Content-Type', 'application/json');
console.log(res.getHeader('Content-Type'));  // 'application/json'

Example Output:

application/json

5. res.getHeaders()

This method retrieves all the headers set for the response in an object, where each header name is a key and its value is the corresponding header value.

Code:

res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'no-store');
console.log(res.getHeaders());

Example Output:

{
  'content-type': 'application/json',
  'cache-control': 'no-store'
}

6. res.getHeaderNames()

This method returns an array of all header names that have been set in the response.

Code:

res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'no-store');
console.log(res.getHeaderNames());

Example Output:

[ 'content-type', 'cache-control' ]

7. res.hasHeader(name)

This method checks if a particular header has been set in the response. It returns true if the header exists, otherwise false.

Code:

res.setHeader('Content-Type', 'application/json');
console.log(res.hasHeader('Content-Type'));  // true
console.log(res.hasHeader('Authorization'));  // false

Example Output:

true
false

8. res.removeHeader(name)

This method removes a specific header from the response.

Code:

res.setHeader('Content-Type', 'application/json');
res.removeHeader('Content-Type');
console.log(res.getHeader('Content-Type'));  // undefined

Example Output:

undefined

9. res.end([data])

This method signals the end of the response. It sends the data (if provided) to the client and ends the response.

Code:

res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!');

Example Output:

Response sent to the client: "Hello, World!"

Full Example Code for res Object

Here's the full code demonstrating how to use the res object in a basic HTTP server. This code handles setting headers, checking headers, sending a response, and more.

const http = require('http');

const server = http.createServer((req, res) => {
  // 1. Set response status code and message
  res.statusCode = 200;
  res.statusMessage = 'OK';

  // 2. Set response headers
  res.setHeader('Content-Type', 'application/json');
  res.setHeader('Cache-Control', 'no-store');

  // 3. Get response headers
  console.log('Headers:', res.getHeaders());  // { 'content-type': 'application/json', 'cache-control': 'no-store' }

  // 4. Get specific header
  console.log('Content-Type:', res.getHeader('Content-Type'));  // 'application/json'

  // 5. Get header names
  console.log('Header Names:', res.getHeaderNames());  // [ 'content-type', 'cache-control' ]

  // 6. Check if header exists
  console.log('Has Content-Type:', res.hasHeader('Content-Type'));  // true
  console.log('Has Authorization:', res.hasHeader('Authorization'));  // false

  // 7. Remove a header
  res.removeHeader('Cache-Control');
  console.log('Headers after removal:', res.getHeaders());  // { 'content-type': 'application/json' }

  // 8. End the response
  res.end('Response sent to the client');
});

server.listen(3000, () => {
  console.log('Server running at http://127.0.0.1:3000/');
});

Full Output Example

When you run the server and visit http://127.0.0.1:3000/, the output in your terminal would look something like this:

Headers: { 'content-type': 'application/json', 'cache-control': 'no-store' }
Content-Type: application/json
Header Names: [ 'content-type', 'cache-control' ]
Has Content-Type: true
Has Authorization: false
Headers after removal: { 'content-type': 'application/json' }
Response sent to the client

Final Project: Real-Life Node.js HTTP Server with Full req and res Functionality

In this final project, we will build a real-life HTTP server using Node.js. The server will demonstrate the usage of all the req (request) and res (response) functionalities that we've discussed.

Project Overview:

This project will include:

  • Handling different HTTP methods (GET, POST).
  • Logging and interacting with various req properties (headers, status codes, query parameters, request body).
  • Manipulating response properties like status codes, headers, and sending responses with different formats.

Full Project Code

const http = require('http');
const url = require('url');

// Create an HTTP server
const server = http.createServer((req, res) => {

  // 1. Log Request Headers
  console.log('Request Headers:', req.headers);

  // 2. Log Request Status Code and Message
  console.log('Request Status Code:', req.statusCode);  // typically used for response
  console.log('Request Status Message:', req.statusMessage);  // typically used for response

  // 3. Log Requested URL
  console.log('Requested URL:', req.url);

  // 4. Handle Query Parameters
  if (req.method === 'GET' && req.url.startsWith('/greet')) {
    const queryObject = url.parse(req.url, true).query;
    const name = queryObject.name || 'Guest';
    console.log('Query Parameter (name):', name);

    // Sending response with query parameter
    res.statusCode = 200;
    res.setHeader('Content-Type', 'application/json');
    res.end(JSON.stringify({ message: `Hello, ${name}!` }));

  // 5. Handle POST request to /submit
  } else if (req.method === 'POST' && req.url === '/submit') {
    let body = [];  // Initialize an empty array to accumulate chunks

    // Collecting body data chunks
    req.on('data', chunk => {
      body.push(chunk);  // Append each chunk to the body array
    });

    req.on('end', () => {
      // Concatenate all the chunks and convert the Buffer to a string
      const bodyString = Buffer.concat(body).toString();
      console.log('Received Body:', bodyString);  // Log the raw body data

      // Try parsing the body if it's JSON
      try {
        const parsedBody = JSON.parse(bodyString);
        const name = parsedBody.name || 'No name provided';
        const age = parsedBody.age || 'No age provided';

        // Set response headers and status code
        res.statusCode = 200;
        res.setHeader('Content-Type', 'application/json');

        // Send JSON response back
        res.end(JSON.stringify({ name, age }));

      } catch (error) {
        // If the body is not in valid JSON format, respond with a 400 error
        res.statusCode = 400;
        res.setHeader('Content-Type', 'application/json');
        res.end(JSON.stringify({ error: 'Invalid JSON format' }));
      }
    });

  // Handle Invalid Routes
  } else {
    res.statusCode = 404;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Not Found');
  }

  // 6. Set Response Headers (Examples)
  res.setHeader('X-Powered-By', 'Node.js');
  res.setHeader('Cache-Control', 'no-store');

  // Log the response headers
  console.log('Response Headers:', res.getHeaders());

  // 7. Log the Status Code and Message for Response
  res.statusCode = 200;
  res.statusMessage = 'OK';
  console.log('Response Status Code:', res.statusCode);  // 200
  console.log('Response Status Message:', res.statusMessage);  // OK

  // 8. Check if a header exists and remove it
  console.log('Has Content-Type header?', res.hasHeader('Content-Type'));  // true
  res.removeHeader('Content-Type');
  console.log('Content-Type after removal:', res.getHeader('Content-Type'));  // undefined

  // Send response
  res.end('Response Sent!');
});

// Start the server
server.listen(3000, () => {
  console.log('Server running at http://127.0.0.1:3000/');
});

Detailed Explanation:

  1. Request Handling and req Object:

    • req.headers: Logs all headers sent by the client.
    • req.statusCode & req.statusMessage: Logs status code and message (though typically used for response, we log here for demonstration).
    • req.url: Logs the full URL path of the incoming request.
    • Query Parameters: Extracts and logs query parameters from the URL using url.parse() method.
    • Body Reading (using Buffer): Handles POST requests by collecting data chunks, concatenating them, and parsing the body using Buffer.concat() to handle the body efficiently.
  2. Response Handling and res Object:

    • res.statusCode & res.statusMessage: Sets and logs the status code and message for the response.
    • res.setHeader(name, value): Sets response headers.
    • res.getHeaders(): Logs all headers set for the response.
    • res.getHeader(name): Retrieves and logs specific headers like Content-Type.
    • res.hasHeader(name): Checks if a particular header is set.
    • res.removeHeader(name): Removes a header from the response.
    • res.end([data]): Ends the response and sends the data to the client.

Example Requests and Responses

  1. Test the GET /greet Route:
  • Request: GET http://127.0.0.1:3000/greet?name=John
  • Response:
   {
     "message": "Hello, John!"
   }
  1. Test the POST /submit Route with Valid JSON:
  • Request: POST http://127.0.0.1:3000/submit with body:
   {
     "name": "Alice",
     "age": 25
   }
  • Response:
   {
     "name": "Alice",
     "age": 25
   }
  1. Test the POST /submit Route with Invalid JSON:
  • Request: POST http://127.0.0.1:3000/submit with body:
   name=John&age=30
  • Response:
   {
     "error": "Invalid JSON format"
   }
  1. Test Invalid Route (404):
  • Request: GET http://127.0.0.1:3000/unknown
  • Response: Not Found

Conclusion

This full project demonstrates how to handle incoming requests and responses using the req and res objects in Node.js. It includes:

  • Logging request properties like headers, status codes, and query parameters.
  • Handling request bodies using buffers and parsing them.
  • Managing response headers, status codes, and sending appropriate responses.

By combining all these functionalities, you get a robust foundation for building more advanced APIs or web servers in Node.js.

版本声明 本文转载于:https://dev.to/harshm03/http-server-nodejs-28d0?1如有侵犯,请联系[email protected]删除
最新教程 更多>
  • Java开发者如何保护数据库凭证免受反编译?
    Java开发者如何保护数据库凭证免受反编译?
    在java 在单独的配置文件保护数据库凭证的最有效方法中存储凭据是将它们存储在单独的配置文件中。该文件可以在运行时加载,从而使登录数据从编译的二进制文件中远离。使用prevereness class import java.util.prefs.preferences; 公共类示例{ 首选项...
    编程 发布于2025-05-09
  • 如何有效地选择熊猫数据框中的列?
    如何有效地选择熊猫数据框中的列?
    在处理数据操作任务时,在Pandas DataFrames 中选择列时,选择特定列的必要条件是必要的。在Pandas中,选择列的各种选项。选项1:使用列名 如果已知列索引,请使用ILOC函数选择它们。请注意,python索引基于零。 df1 = df.iloc [:,0:2]#使用索引0和1 c...
    编程 发布于2025-05-09
  • 为什么使用Firefox后退按钮时JavaScript执行停止?
    为什么使用Firefox后退按钮时JavaScript执行停止?
    导航历史记录问题:JavaScript使用Firefox Back Back 此行为是由浏览器缓存JavaScript资源引起的。要解决此问题并确保在后续页面访问中执行脚本,Firefox用户应设置一个空功能。 警报'); }; alert('inline Alert')...
    编程 发布于2025-05-09
  • 哪种在JavaScript中声明多个变量的方法更可维护?
    哪种在JavaScript中声明多个变量的方法更可维护?
    在JavaScript中声明多个变量:探索两个方法在JavaScript中,开发人员经常遇到需要声明多个变量的需要。对此的两种常见方法是:在单独的行上声明每个变量: 当涉及性能时,这两种方法本质上都是等效的。但是,可维护性可能会有所不同。 第一个方法被认为更易于维护。每个声明都是其自己的语句,使其...
    编程 发布于2025-05-09
  • Go语言如何动态发现导出包类型?
    Go语言如何动态发现导出包类型?
    与反射软件包中的有限类型的发现能力相反,本文探索了替代方法,探索了在Runruntime。go import( “ FMT” “去/进口商” ) func main(){ pkg,err:= incorter.default()。导入(“ time”) 如果err...
    编程 发布于2025-05-09
  • 如何有效地转换PHP中的时区?
    如何有效地转换PHP中的时区?
    在PHP 利用dateTime对象和functions DateTime对象及其相应的功能别名为时区转换提供方便的方法。例如: //定义用户的时区 date_default_timezone_set('欧洲/伦敦'); //创建DateTime对象 $ dateTime = ne...
    编程 发布于2025-05-09
  • 如何使用不同数量列的联合数据库表?
    如何使用不同数量列的联合数据库表?
    合并列数不同的表 当尝试合并列数不同的数据库表时,可能会遇到挑战。一种直接的方法是在列数较少的表中,为缺失的列追加空值。 例如,考虑两个表,表 A 和表 B,其中表 A 的列数多于表 B。为了合并这些表,同时处理表 B 中缺失的列,请按照以下步骤操作: 确定表 B 中缺失的列,并将它们添加到表的末...
    编程 发布于2025-05-09
  • 如何使用PHP从XML文件中有效地检索属性值?
    如何使用PHP从XML文件中有效地检索属性值?
    从php $xml = simplexml_load_file($file); foreach ($xml->Var[0]->attributes() as $attributeName => $attributeValue) { echo $attributeName,...
    编程 发布于2025-05-09
  • PHP阵列键值异常:了解07和08的好奇情况
    PHP阵列键值异常:了解07和08的好奇情况
    PHP数组键值问题,使用07&08 在给定数月的数组中,键值07和08呈现令人困惑的行为时,就会出现一个不寻常的问题。运行print_r($月份)返回意外结果:键“ 07”丢失,而键“ 08”分配给了9月的值。此问题源于PHP对领先零的解释。当一个数字带有0(例如07或08)的前缀时,PHP将...
    编程 发布于2025-05-09
  • Python元类工作原理及类创建与定制
    Python元类工作原理及类创建与定制
    python中的metaclasses是什么? Metaclasses负责在Python中创建类对象。就像类创建实例一样,元类也创建类。他们提供了对类创建过程的控制层,允许自定义类行为和属性。在Python中理解类作为对象的概念,类是描述用于创建新实例或对象的蓝图的对象。这意味着类本身是使用类关...
    编程 发布于2025-05-09
  • 可以在纯CS中将多个粘性元素彼此堆叠在一起吗?
    可以在纯CS中将多个粘性元素彼此堆叠在一起吗?
    [2这里: https://webthemez.com/demo/sticky-multi-header-scroll/index.html </main> <section> { display:grid; grid-template-...
    编程 发布于2025-05-09
  • 如何检查对象是否具有Python中的特定属性?
    如何检查对象是否具有Python中的特定属性?
    方法来确定对象属性存在寻求一种方法来验证对象中特定属性的存在。考虑以下示例,其中尝试访问不确定属性会引起错误: >>> a = someClass() >>> A.property Trackback(最近的最新电话): 文件“ ”,第1行, AttributeError: SomeClass...
    编程 发布于2025-05-09
  • 您可以使用CSS在Chrome和Firefox中染色控制台输出吗?
    您可以使用CSS在Chrome和Firefox中染色控制台输出吗?
    在javascript console 中显示颜色是可以使用chrome的控制台显示彩色文本,例如红色的redors,for for for for错误消息?回答是的,可以使用CSS将颜色添加到Chrome和Firefox中的控制台显示的消息(版本31或更高版本)中。要实现这一目标,请使用以下模...
    编程 发布于2025-05-09
  • 在JavaScript中如何获取实际渲染的字体,当CSS字体属性未定义时?
    在JavaScript中如何获取实际渲染的字体,当CSS字体属性未定义时?
    Accessing Actual Rendered Font when Undefined in CSSWhen accessing the font properties of an element, the JavaScript object.style.fontFamily and objec...
    编程 发布于2025-05-09
  • 如何使用node-mysql在单个查询中执行多个SQL语句?
    如何使用node-mysql在单个查询中执行多个SQL语句?
    Multi-Statement Query Support in Node-MySQLIn Node.js, the question arises when executing multiple SQL statements in a single query using the node-mys...
    编程 发布于2025-05-09

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3