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

Xây dựng URL trong Express - Freetuts
EXPRESS FRAMEWORK
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

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 dựng URL trong Express, phần này sẽ phụ thuộc rất nhiều về phần Routes trong Express, các bạn hãy tham khảo bài viết đó trước khi tiếp tục bài viết này 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.

Trong Express bạn có thể khai báo đường dẫn cố định hoặc đường dẫn động, việc sử dụng đường dẫn động giúp chúng ta có thể điều hướng người dùng tới nhiều nơi hơn. Ví dụ ở đây mình có URL tĩnh:

https://example.com/admin

và đây là đường dẫn động:

https://example.com/posts/1
https://example.com/posts/2
https://example.com/posts/3

Nội dung bài viết sẽ chủ yếu tập chung ra những cách khởi tạo đường dân động (dynamic URL) .

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

1. Đường dẫn tĩnh (Static URL)

Ở đây mình có một đường dẫn tĩnh, để khai báo trong Express bạn chỉ thêm chỉ định route patch là path mà bạn muốn khởi tạo. Chúng ta sẽ có ví dụ như sau :

var express = require('express');
var app = express();

app.get('/admin', function(req, res){
   res.send('Admin Page');
});
app.listen(3000);

Tiến hành khởi chạy đường dẫn sẽ hiển thị như sau:

freetuts url building express png

Đây là một phần khá đơn giản nên mình chỉ tập chung vào dynamic URL bên duới.

2. Đường dẫn động (Dymanic URL)

Route paths có thể là một string, string patterns hay regular express, chúng ta có thể khởi tạo một Dymanic URL bằng cách sử dụng regular express patch và string paterns.

Các ký tự như ?, +, *() là cách ký tự regular express hay được sử dụng. Ký tự ngạch ngang (-) và chấm (.) được mặc định là một string-based path.

Route parameters

Tham số được đặt tên trong các phân đoạn URL được sử dụng để bắt các giá trị được chỉ định tại vị trí của chúng trong URL. Các giá trị đã ghi vào đối tượng req.params, với key của tham số tuyến được chỉ định trong đường dẫn, và value là giá trị tương ứng của chúng.

Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }

Để chỉ định một route parameters chúng ta sử dụng cú pháp sau:

app.get('/users/:userId/books/:bookId', function (req, res) {
  res.send(req.params)
})

Ngoài ra bạn còn có thể thêm pattern match cho router params :

app.get('/things/:id([0-9]{5})', function(req, res){
   res.send('id: ' + req.params.id);
});

Dưới đây mình có môt ví dụ:

var express = require('express');
var app = express();

app.get('/members/:memberID([0-9]{2})', function(req, res){
   res.send(req.params);
});
app.listen(3000);

freetuts url building express 2 png

Lưu ý rằng các params name phải là ký tự từ ([A-Za-z0-9_]).

Pattern Matched Routes

Bạn cũng có thể sử dụng string paterns trong paths, ở đây mình có một vài ví dụ :

Route này sẽ có path là acd hoặc abcd

app.get('/ab?cd', function (req, res) {
  res.send('ab?cd')
})

Route này sẽ có path là abcd, abbcd, abbbcd,vv...

app.get('/ab+cd', function (req, res) {
  res.send('ab+cd')
})

Route này sẽ có path là abcd, abxcd, abRANDOMcd, ab123cd,vv...

app.get('/ab*cd', function (req, res) {
  res.send('ab*cd')
})

Route này sẽ có path là /abe /abcde

app.get('/ab(cd)?e', function (req, res) {
  res.send('ab(cd)?e)
})

Regular expressions Routes

Ngoài ra, bạn còn có thể sử dụng regular express trong path route. Ở đây mình có một vài ví dụ :

Chỉ match với kí tự a.

app.get(/a/, function (req, res) {
  res.send('/a/')
})

Match với số điện thoại:

app.get('/phonenumbers/((09|03|07|08|05)+([0-9]{8}))', function(req, res){
   res.send(req.params);
});

freetuts url building express 3 png

Trong bài này chúng ta đã cùng nhau đi tìm hiểu về xây dựng URL trong Express, đây là kiến thức rất cơ bản về nó nhưng cũng hết sức quan trọng trong quá trình làm việc với Express. Mong rằng bài viết sẽ giúp ích cho bạn

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

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

Insert / Update / Delete / Select MySQL trong NodeJS

Insert / Update / Delete / Select MySQL trong NodeJS

Top