Home > Blockchain >  need to account for params being blank in node express app
need to account for params being blank in node express app

Time:07-13

i have a node/express app, and need to account for if users don't enter a stationId param in the Url. Ive been looking at all sorts of regex built in to express, adding express-validator but couldnt get it to work for middleware. what would be the best way to do this? is the something in express or a third party validator?

The stationId is an mix of letters and numbers 1491TH it is used to call a second API to get information about the station entered in the Url. Im trying to work out how to use a validator to check if the param is blank, or if its not in the format i want.

import express from 'express'
import axios from 'axios'
import { cleanseLocation } from './utils.js'


const PORT = 3000
const app = express()

app.get('/:stationId/asset',(req, res) => {

 const stationId = cleanseLocation(req.params.stationId)

 const resp = await axios.get(`https://online-api/id/stations/${stationId}`)
   res.send(resp)
})

app.listen(PORT, () =>
  console.log(`The node API is running on: http://localhost:${PORT}.`)
)

CodePudding user response:

When you define a route like:

app.get('/:stationId/asset', ...)

You are using a wildcard for the first path segment. It will match ANYTHING in the first path segment. So, it will match all of these:

/play/asset
/1238576/asset
/ /asset

It will not match:

/asset

because that's just one path segment.

If you have specific rules for what is and isn't a valid stationId, you can implement a check for those inside the route handler:

app.get('/:stationId/asset',(req, res) => {
   if (some logic to check req.param.stationId) {
       res.send('working');
   } else {
       res.status(404).send("Invalid stationId");
   }
});

If you need further help with how to implement stationId checking, you will have to disclose exactly how you would tell if it's a valid stationId or not.

is the something in express or a third party validator?

What mechanism to use for implementing validation depends entirely upon how you determine whether it is or isn't a valid stationId. You would have to explain that algorithm or method for us to help further.

The stationId is an mix of letters and numbers 1491TH it is used to call a second API to get information about the station entered in the Url. Im trying to work out how to use a validator to check if the param is blank, or if its not in the format i want

It already can't be blank. It won't match the route if there's no stationId at all.

So, if you are looking for a sequence of letters and numbers, you can just use a regex and define the route such that it will only match the route definition if you get a legal format for a stationId:

app.get('/:stationId([A-Za-z0-9] )/asset', ...)

FYI, here's a helpful article with good example on using regex in Express routes. The full doc for what you can do with a regex in a route definition is here in the path-to-regexp documentation which is the module Express uses for this feature.

  • Related