- Node.js Blueprints
- Krasimir Tsonev
- 387字
- 2021-07-16 11:36:15
Handling dynamic URLs and the HTML forms
The Express framework also supports dynamic URLs. Let's say we have a separate page for every user in our system. The address to those pages looks like the following code:
/user/45/profile
Here, 45
is the unique number of the user in our database. It's of course normal to use one route handler for this functionality. We can't really define different functions for every user. The problem can be solved by using the following syntax:
var getUser = function(req, res, next) { res.send("Show user with id = " + req.params.id); } app.get('/user/:id/profile', getUser);
The route is actually like a regular expression with variables inside. Later, that variable is accessible in the req.params
object. We can have more than one variable. Here is a slightly more complex example:
var getUser = function(req, res, next) { var userId = req.params.id; var actionToPerform = req.params.action; res.send("User (" + userId + "): " + actionToPerform) } app.get('/user/:id/profile/:action', getUser);
If we open http://localhost:3000/user/451/profile/edit
, we see User (451): edit
as a response. This is how we can get a nice looking, SEO-friendly URL.
Of course, sometimes we need to pass data via the GET or POST parameters. We may have a request like http://localhost:3000/user?action=edit
. To parse it easily, we need to use the native url
module, which has few helper functions to parse URLs:
var getUser = function(req, res, next) { var url = require('url'); var url_parts = url.parse(req.url, true); var query = url_parts.query; res.send("User: " + query.action); } app.get('/user', getUser);
Once the module parses the given URL, our GET parameters are stored in the .query
object. The POST variables are a bit different. We need a new middleware to handle that. Thankfully, Express has one, which is as follows:
app.use(express.bodyParser()); var getUser = function(req, res, next) { res.send("User: " + req.body.action); } app.post('/user', getUser);
The express.bodyParser()
middleware populates the req.body
object with the POST data. Of course, we have to change the HTTP method from .get
to .post
or .all
.
If we want to read cookies in Express, we may use the cookieParser
middleware. Similar to the body parser, it should also be installed and added to the package.json
file. The following example sets the middleware and demonstrates its usage:
var cookieParser = require('cookie-parser'); app.use(cookieParser('optional secret string')); app.get('/', function(req, res, next){ var prop = req.cookies.propName });
- 玩轉Scratch少兒趣味編程
- TypeScript Essentials
- Mastering Kotlin
- Spring Boot+Spring Cloud+Vue+Element項目實戰:手把手教你開發權限管理系統
- x86匯編語言:從實模式到保護模式(第2版)
- 信息安全技術
- PHP+MySQL+Dreamweaver動態網站開發實例教程
- 可解釋機器學習:模型、方法與實踐
- Moodle 3 Administration(Third Edition)
- Java從入門到精通(視頻實戰版)
- 從“1”開始3D編程
- VMware vSphere 5.5 Cookbook
- Oracle SOA Suite 12c Administrator's Guide
- 第五空間戰略:大國間的網絡博弈
- Learning Puppet