Express handle routes with regex - express

I have an route like as '/antalya-ucak-bileti' and 'antalya' word will be dynamically change. How can I handle it in routes?
I'm used NodeJS and ExpressJS
Thanks

This should work:
app.get('/:destination-ucak-bileti', (req, res) => {
console.log('P', req.params.destination);
res.end();
});

Related

How do you access previous params in router file in express.js?

let's say I create a router like so in the app file:
const usernameRouter = express.Router();
app.use('/:username', usernameRouter);
When I'm in the router file, how would access that :username variable?
There is no built in way to get that parameter from the sub-route. You'd have several options:
1) Use req.originalUrl
In your sub-route handler, parse it out of req.originalUrl.
2) Move the /:username into the route declaration
Don't use a wildcard when sending to the router. Instead, just do this:
app.use(usernameRouter);
And, then inside of usernameRouter, do this:
router.get("/:username/something", ...);
So, you can then use req.params.username to get access to that.
3) Create middleware to capture req.params.username
Use a middleware function to set the parameter to a place you can get to it:
app.use('/:username', (req, res, next) => {
req.username = req.params.username;
usernameRouter(req, res, next);
});
Then, you can access it from req.username in the sub-routes.

Express route with optional parameter?

I have an Express route for public-sitemap.xml:
app.get("/public-sitemap.xml", function(req, res) {
// do stuff
});
I'd like to support URLs like public-sitemap-1.xml, public-sitemap-2.xml as well.
How can I allow these optional parameters in Express?
If I set the route to /public-sitemap-?:id?.xml that allows public-sitemap-1.xml etc, but it also allows public-sitemap1.xml, which I'd prefer to reject.
Is this what you're looking for?
/public-sitemap(-:id?)?.xml
More info here: https://expressjs.com/en/guide/routing.html
Use this:
app.get("/public-sitemap-:id(\\d+).xml", function(req, res) {
// do stuff
});

VueRouter make HTTP request within beforeEach

I am attempting to make an AXIOS request within router.beforeEach. However, it looks like the request is being made with my next destination URL being prepended; if trying to access /client/create, the beforeEach appears to prepend '/client/create' to the request.
Instead of '/api/participant/test/{some_id}' the request is being sent to '/client/create/api/participant/{some_id}'.
I'm not quite sure why this is happening. The documentation indicates that you could use a getPost() method to make requests:
beforeRouteEnter (to, from, next) {
getPost(to.params.id, (err, post) => {
next(vm => vm.setData(err, post))
})
},
However it seems the getPost() method is unrecognized, which could be because of the beforeEach call (the documentation does not show that this could be used with this particular method).
Here is the code with the AXIOS request.
router.beforeEach((to, from, next) => {
console.log(to.params.id);
// Check to see if the cookie exists
if (document.cookie.match(/^(.*;)?\s*participant_token\s*=\s*[^;]+(.*)?$/)) {
axios.get('api/participant/test/' + to.params.id)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
}
Any ideas on how this can be avoided? I imagine one could use beforeRouteEnter for each of the routes I have set up, but this would be a more elegant solution.
It should be
axios.get('/api/participant/test/' + to.params.id)
(with forward slash character at the beginner).
The more properly way to set baseURL in axios config
For example:
axios.defaults.baseURL = 'your base url';
First, there is no built-in getPost method in Vue.js. Documentation has mentioned it just as an illustration purpose.
Also, use root relative URL instead of relative URL that you are trying to use.
axios.get('/api/participant/test/' + to.params.id)
You are trying to use relative URL which is causing a problem for you. The more generic way would be to set default base URL in Axios global config.

ExpressJS Router URL Matching

code below, when I visit url like this http://localhost/. It can match the first one, but when I visit http://localhost/detail-999, It match the first one again. It can not match the second one.
I want that when I visit http://localhost or http://localhost/list-1-1 can match the first one and when visit http://localhost/detail-999 can match the second one correctly...
I have no idea to fix this problem...
router.get('/|/list-:type-:page', function (req, res) {});
router.get('/detail-:itemId', function (req, res) {});
All you need is to wrap it to brackets like this:
app.get('(/|/list-:type-:page)', function (req, res) {
});
Try this:
router.get('/list/:type/:page', function (req, res, next) {});
router.get('/detail/:itemId', function (req, res, next) {});
It can be problematic designing your routes as you did. If you have params that can not be changed then you should handle the dashes in the route's action method and just do a req.params.list_name.split('-')

Express: use separate route file for multiple kinds of http requests on same path

Here are my routes:
app.get('/signUp', routes.signUp);
app.post('/signUp' , routes.signUp);
Here is my separate file for routes.
exports.signUp = function(req, res) {
res.render('signUp');
};
The second block of code is behaviour I want in response to a get request.
How do I respond to a post request? I have already tied up the signUp function with behaviour that responds to get. Do I bundle up the post behaviour in the same function and render the sign up page again? Suppose I simply want to render the view, I don't want the post behaviour to execute in that case so it would be strange to bundle those together.
I believe the express router module should resolve this for you.
route file -
var express = require('express');
var router = express.Router();
router.route("/")
.get(function (req, res) {
res.render('signUp');
})
.post(function (req, res) {
//do something else
})
module.exports = router
index.js/app.js/server.js/whatever you call it.
//..
signUp = require("./routes/signup.js"); //or wherever this is
//...
app.use("/signUp", signUp);
//..