- 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 });
- Python 3.7網(wǎng)絡(luò)爬蟲快速入門
- Python自然語言處理實(shí)戰(zhàn):核心技術(shù)與算法
- 騰訊iOS測(cè)試實(shí)踐
- JMeter 性能測(cè)試實(shí)戰(zhàn)(第2版)
- oreilly精品圖書:軟件開發(fā)者路線圖叢書(共8冊(cè))
- Visual Basic程序設(shè)計(jì)習(xí)題解答與上機(jī)指導(dǎo)
- Python機(jī)器學(xué)習(xí)編程與實(shí)戰(zhàn)
- WebRTC技術(shù)詳解:從0到1構(gòu)建多人視頻會(huì)議系統(tǒng)
- Android底層接口與驅(qū)動(dòng)開發(fā)技術(shù)詳解
- Illustrator CS6設(shè)計(jì)與應(yīng)用任務(wù)教程
- Flutter從0基礎(chǔ)到App上線
- Mobile Test Automation with Appium
- Web前端開發(fā)精品課:HTML5 Canvas開發(fā)詳解
- Python Business Intelligence Cookbook
- Mathematica Data Visualization