javascript - Route parameters in Express JS - Stack Overflow

I am using Express JS to handle a route http:localhost:3000location which allows a mix of parameters

I am using Express JS to handle a route http://localhost:3000/location which allows a mix of parameters and fixed endpoints. For example:

http://localhost:3000/location is the root for the route, which renders a view for a list of locations.

http://localhost:3000/location/map renders a view for a list of the locations drawn on a web map.

http://localhost:3000/location/:id contains a parameter for the ID of a location given in the URL and when called, renders a view for the details of the given location that es from a database query.

'use strict';

var path = require('path');
var express = require('express');
var router = express.Router();

/* GET route root page. */
router.get('/', function(req, res, next) {
  // DO SOMETHING
});

/* GET the map page */
router.get('/map', function(req, res, next) {
  // DO SOMETHING
});

/* GET individual location. */
router.get('/:id', function(req, res, next) {
  // DO SOMETHING
});

module.exports = router;

Is this a best practise for handling a route with mixed fixed values and parameterized parameters?

More specifically, how to properly handle the problem that when I called "http://localhost:3000/location/SOMETHINGWRONG", for example, http://localhost:3000/location/:id was triggered which led to a database query error because "SOMETHINGWRONG" was not an integer and could not pass?

I am using Express JS to handle a route http://localhost:3000/location which allows a mix of parameters and fixed endpoints. For example:

http://localhost:3000/location is the root for the route, which renders a view for a list of locations.

http://localhost:3000/location/map renders a view for a list of the locations drawn on a web map.

http://localhost:3000/location/:id contains a parameter for the ID of a location given in the URL and when called, renders a view for the details of the given location that es from a database query.

'use strict';

var path = require('path');
var express = require('express');
var router = express.Router();

/* GET route root page. */
router.get('/', function(req, res, next) {
  // DO SOMETHING
});

/* GET the map page */
router.get('/map', function(req, res, next) {
  // DO SOMETHING
});

/* GET individual location. */
router.get('/:id', function(req, res, next) {
  // DO SOMETHING
});

module.exports = router;

Is this a best practise for handling a route with mixed fixed values and parameterized parameters?

More specifically, how to properly handle the problem that when I called "http://localhost:3000/location/SOMETHINGWRONG", for example, http://localhost:3000/location/:id was triggered which led to a database query error because "SOMETHINGWRONG" was not an integer and could not pass?

Share Improve this question asked Jul 2, 2018 at 23:21 alextcalextc 3,52516 gold badges71 silver badges119 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You can restrict a rule with regex in your route, for example, if you only expect to receive whole numbers, you can use something like this:

router.get('/:id(\\d{12})', (req, res) => {
  //...
});

Enter the method if you meet the rule, where "id" is a number with 12 characters

Validate only numbers:

app.get('/:id(\\d+)', function (req, res){ 
  //...
});

To have more control over the exact string that can be matched by a route parameter, you can append a regular expression in parentheses (()). ex: Your Id is an integer with a maximum length of 10 characters

/* GET individual location. */
router.get('/:id([0-9]{1,10})', function(req, res, next) {
  // DO SOMETHING
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742318189a4421265.html

相关推荐

  • javascript - Route parameters in Express JS - Stack Overflow

    I am using Express JS to handle a route http:localhost:3000location which allows a mix of parameters

    17小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信