I learn expressjs, in which I pass the value, but do not come into the output.I have created a new Routes file
and want to be a function with Route methods
, so this is done. But then if I set Route paths in it, I don't get its value in the output. This route path
will match, but value not get.. So help me and Solve.
This is my Routes file (user.js
)
var express = require('express')
var app = express()
app.get('/user/pq*xd', function(req, res) {
res.send("User Data accessed: "+req.params)
});
module.exports = app
URL :- http://localhost:3000/user/pq15A5yxd
This case output is :- User Data accessed:
It does not display url value. How to solve this?
req.params
This value to return Object So try req.params[0]
to get URL value..
/Yout code is...
var express = require('express')
var app = express()
app.get('/user/pq*xd', function(req, res) {
res.send("User Data accessed: "+req.params[0])
});
module.exports = app
Browser URL:- http://localhost:3000/user/pq15A5yxd
The output is:- User Data accessed: 15A5y
Routing refers to how an application’s endpoints (URIs) respond to client requests. For an introduction to routing, see Basic routing.
Route methods
A route method is derived from one of the HTTP methods, and is attached to an instance of the express class. The following code is an example of routes that are defined for the GET and the POST methods to the root of the app.
// GET method route
app.get('/', function (req, res) {
res.send('GET request to the homepage')
})
// POST method route
app.post('/', function (req, res) {
res.send('POST request to the homepage')
})
Route paths
Route paths, in combination with a request method, define the endpoints at which requests can be made. Route paths can be strings, string patterns, or regular expressions. The characters ?, +, *, and () are subsets of their regular expression counterparts. The hyphen (-) and the dot (.) are interpreted literally by string-based paths.
Here are some examples of route paths based on string patterns.
This route path will match acd and abcd.
app.get('/ab?cd', function (req, res) {
res.send('ab?cd')
})
For more information click here.