peri hokiperihokiclarustologisticsrimashaop.comangelpaintingtools.co.invrjanaenergies.comgates of olympus perihoki jadi mesin uang paling gacortrik main baccarat perihoki ala pro bikin pemula jadi jutawanpantang pulang sebelum jackpot main dadu sicbo perihokipgsoft mahjong ways 2 muncul scatter merah tiap spin perihokicara menciptakan akun perihokigacor mahjong wins 3jackpot kembar mahjong wins 3 abc1131cara menang stabil rtp mahjong ways abc1131bu nurlina menang 197 juta mahjong ways 2mahasiswa cuan 421 juta karena pola mahjongpemilik warung menang 128 juta mahjong wins 3strategi bravy erika mahjong abc1131 terungkappemuda jambi taklukkan mahjong wins 3penjual jamu menang besar mahjong wins 5pemuda kampung raup 180 juta mahjong wayskisah inspiratif petani mahjong ways keberhasilanmahasiswa menang 92 juta mahjong waysmodal warung kopi bang ojak sukses mahjong waystanpa modal maxwin mahjong wins 3 strategibocoran mahjong ways spin kombinasicara bermain mahjong untuk meraih keberuntungankeajaiban mahjong kasir indomaret sukses beli tanahmeraup keuntungan maksimal di mahjong winsid rahasia scatter hitamid pola scatterid jackpot mahjong waysid spin mahjong winsid tips jitu mahjongrahasia kemenangan mahjong ways spin slow

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Hàm writeHead và setHeader trong NodeJS - Freetuts
CĂN BẢN
SERVER
MYSQL
MONGODB
MODULE
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Hàm writeHead và setHeader trong NodeJS

Ở bài trước mình có hướng dẫn tạo server NodeJS đơn giản và trong bài mình có sử dụng hàm writeHead để thêm các tham số vào header, vậy thì trong bài này mình sẽ giải thích kỹ hơn về hàm này và cách sử dụng nó. Và trước khi đọc bài này thì bạn phải hiểu Header Request và Header Response đã nhé.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

1. Hàm response.writeHead trong NodeJS

Hàm response.writeHead dùng để thiết lập các tham số của header khi gửi kết quả về cho client, cú pháp của nó như sau:

response.writeHead(statusCode, headerObject);

Bài viết này được đăng tại [free tuts .net]

Trong đó statusCode là mã code gồm 3 chữ số, đây là mã số quyết định trạng thái của request.

  • 100 Continue
  • 101 Switching Protocols
  • 2xx Success
  • 200 Ok
  • 201 Created
  • Xem thêm tại đây

Trong ví dụ này mình sử dụng mã code 200 để thông báo là thao tác thành công.

var http = require('http');

// Bước 2: Khởi tạo server
var server = http.createServer(function(request, response){
    // Thiết lập Header
    response.writeHead(200, {
        "Context-type" : "text/html"
    });
    response.end();
});

// Bước 3: Lắng nghe cổng 300 thì thực hiện chương trình
server.listen(3000, function(){
    console.log('Connected Successfull!');
});

Chạy đoạn code này lên và sử dụng firebug bạn xem kết quả sẽ thấy các thông số như sau:

response writehead nodejs png

Mình đã sử dụng mã code 200 và thiết lập cho ba thông số Content-type, author, blog như phần bôi đỏ trong hình.

Bây giờ bạn đổi lại statusCode là 404 như sau:

var http = require('http');

// Bước 2: Khởi tạo server
var server = http.createServer(function(request, response){
    // Thiết lập Header
    response.writeHead(404, {
        "Context-type"  : "text/html",
        "author"        : "thehalfheart@gmail.com",
        "blog"          : "freetuts.net"
    });
    response.end();
});

// Bước 3: Lắng nghe cổng 300 thì thực hiện chương trình
server.listen(3000, function(){
    console.log('Connected Successfull!');
});

Thì trạng thái sẽ chuyển sang màu đỏ (404) thật là nguy hiểm:

response writehead nodejs 1 png

2. Hàm response.setHeader trong NodeJS

Hàm này có chức năng tương tự như hàm response.writeHeader. Bạn sửa lại ví dụ trên như sau:

var http = require('http');

// Bước 2: Khởi tạo server
var server = http.createServer(function(request, response){
    // Thiết lập Header
    response.setHeader('Context-type', 'text/html');
    response.setHeader('author', 'thehalfheart@gmail.com');
    response.setHeader('blog', 'freetuts.net');
    response.writeHead(200, {});
    response.end();
});

// Bước 3: Lắng nghe cổng 300 thì thực hiện chương trình
server.listen(3000, function(){
    console.log('Connected Successfull!');
});

Kết quả cũng như ví dụ ở phần 1.

response writehead nodejs png

3. Lưu ý khi thiết lập Header

Cũng như trong PHP vậy thôi, ban phải thiết lập header ở phía trên cùng của chương trình (phía trên code), nghĩa là nó phải được đặt trước các đoạn code như response.write(), response.end().

Khi nào thì sử dụng hai hàm trên? Câu trả lời tùy bạn thôi, bạn thích sử dụng hàm nào cũng được. Tuy nhiên có một lưu ý là vấn đề ghi đè, nghĩa là hàm response.writeHead sẽ ghi đè các tham số đã được thiết lập ở hàm response.setHeader nếu chúng trùng nhau.

Ví dụ: Ghi đè freetuts.net thành https://freetuts.net

var http = require('http');

// Bước 2: Khởi tạo server
var server = http.createServer(function(request, response){
    // Thiết lập Header
    response.setHeader('Context-type', 'text/html');
    response.setHeader('author', 'thehalfheart@gmail.com');
    response.setHeader('blog', 'freetuts.net');
    response.writeHead(200, {
        "blog" : "https://freetuts.net"
    });
    response.end();
});

// Bước 3: Lắng nghe cổng 300 thì thực hiện chương trình
server.listen(3000, function(){
    console.log('Connected Successfull!');
});

Kết quả:

response writehead nodejs 2 png

4. Lời kết

Như vậy là mình đã giới thiệu một số điểm quan trọng của hai hàm setHeader và writeHead trong NodeJS. Cá nhân mình thì thường sử dụng kết hợp cả hai hàm trên vì môi hàm có một lợi thế khác nhau.

Thực ra vấn đề này bạn cũng có thể tự tìm hiểu được nhưng mình muốn mọi thứ rõ ràng hơn nên mới viết bài này, qua bài này cũng sẽ đở mất nhiều thời gian giải thích hơn. Bài này mình kết thúc tại đây, chào hẹn gặp lại.

Cùng chuyên mục:

Cách gửi mail trong NodeJS có file đính kèm

Cách gửi mail trong NodeJS có file đính kèm

Crawl dữ liệu website bằng NodeJS cơ bản

Crawl dữ liệu website bằng NodeJS cơ bản

CORS là gì ? Xử lý CORS trong NodeJS

CORS là gì ? Xử lý CORS trong NodeJS

Chắc hẳn trong quá trình xử lý các request tới server thì một vài trường…

Xử lý form trong Express

Xử lý form trong Express

Xây dựng URL trong Express

Xây dựng URL trong Express

Trong bài viết này chúng ta sẽ cùng nhau đi tìm hiểu về cách xây…

Populate trong Mongoose

Populate trong Mongoose

Error Handling & Debugging trong Socket.io

Error Handling & Debugging trong Socket.io

Namespaces & Rooms trong Socket.io

Namespaces & Rooms trong Socket.io

Middleware trong Mongoose

Middleware trong Mongoose

Create / Read / Update / Delete trong Mongoose

Create / Read / Update / Delete trong Mongoose

Validation trong Mongoose

Validation trong Mongoose

Subdocuments trong Mongooose

Subdocuments trong Mongooose

Documents trong Mongoose

Documents trong Mongoose

Trong bài viết này chúng ta sẽ tìm hiểu về documents trong Mongoose

Models trong Mongoose

Models trong Mongoose

SchemaTypes trong Mongoose

SchemaTypes trong Mongoose

Schemas trong Mongoose

Schemas trong Mongoose

Mongoose trong NodeJS

Mongoose trong NodeJS

Làm việc với Table MySQL sử dụng Nodejs

Làm việc với Table MySQL sử dụng Nodejs

Kết nối với MongoDB trong NodeJS

Kết nối với MongoDB trong NodeJS

Giới thiệu MongoDB trong NodeJS

Giới thiệu MongoDB trong NodeJS

Top