http.createServer([options][, requestListener])
-
options
<Object>IncomingMessage
<http.IncomingMessage> 指定要使用的IncomingMessage
类。 用于扩展原始的IncomingMessage
。 默认值:IncomingMessage
。ServerResponse
<http.ServerResponse> 指定要使用的ServerResponse
类。 用于扩展原始的ServerResponse
。 默认值:ServerResponse
。insecureHTTPParser
<boolean> 使用不安全的 HTTP 解析器,当为true
时接受无效的 HTTP 标头。 应避免使用不安全的解析器。 有关详细信息,请参阅--insecure-http-parser
。 默认值:false
maxHeaderSize
<number> 可选地覆盖此服务器接收到的请求的--max-http-header-size
值,即请求头的最大长度(以字节为单位)。 默认值: 16384 (16KB).
-
requestListener
<Function> -
返回: <http.Server>
返回 http.Server
的新实例。
requestListener
是自动添加到 'request'
事件的函数。
const http = require('http');
// 创建本地服务器来从其接收数据
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
data: 'Hello World!'
}));
});
server.listen(8000);
const http = require('http');
// 创建本地服务器来从其接收数据
const server = http.createServer();
// 监听请求事件
server.on('request', (request, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
data: 'Hello World!'
}));
});
server.listen(8000);
-
options
<Object>IncomingMessage
<http.IncomingMessage> Specifies theIncomingMessage
class to be used. Useful for extending the originalIncomingMessage
. Default:IncomingMessage
.ServerResponse
<http.ServerResponse> Specifies theServerResponse
class to be used. Useful for extending the originalServerResponse
. Default:ServerResponse
.insecureHTTPParser
<boolean> Use an insecure HTTP parser that accepts invalid HTTP headers whentrue
. Using the insecure parser should be avoided. See--insecure-http-parser
for more information. Default:false
maxHeaderSize
<number> Optionally overrides the value of--max-http-header-size
for requests received by this server, i.e. the maximum length of request headers in bytes. Default: 16384 (16KB).
-
requestListener
<Function> -
Returns: <http.Server>
Returns a new instance of http.Server
.
The requestListener
is a function which is automatically
added to the 'request'
event.
const http = require('http');
// Create a local server to receive data from
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
data: 'Hello World!'
}));
});
server.listen(8000);
const http = require('http');
// Create a local server to receive data from
const server = http.createServer();
// Listen to the request event
server.on('request', (request, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
data: 'Hello World!'
}));
});
server.listen(8000);