How to Force Node.Js Require to Be Case Sensitive

Case sensitive issue in node.js

Firstly, for compatibility reason, macOS chose a case-insensitive file system. But it doesn't mean you have to bear it, Disk Utility can be used to reformat the partition to case-sensitive mode. If You do that, node.js will report error to you because of the wrong module name you are trying to require.

Then, let's talk about your test result. The key issue is which one you require in duplicatedError.js, if you change it a little bit:

//change the required name to lowercase extendableError
const ExtendableError = require('./extendableError')

class DuplicatedError extends ExtendableError {
constructor(message) {
super(message)
}
}

module.exports = DuplicatedError

The test result will be:

false
true

You can even try to modify duplicatedError.js like below:

//change the required name to extENDableError
const ExtendableError = require('./extENDableError')

class DuplicatedError extends ExtendableError {
constructor(message) {
super(message)
}
}

module.exports = DuplicatedError

The result shall be

false
false

So i think it's not about module caching, you have two things to be clear here:

  1. macOX is case-insensitive file system by default
  2. even you have only one file extendableError.js, but you require twice with different names, like: require(./extendableError), require(./ExtendableError), require(./extENDableError), trey will be treated as three modules.

Case sensitivity of Node.js Express static assets

I was given also some advice for a simple alternative solution:

Set a policy that all your static files and paths should be in lowercase.

Enforce the policy on the files by adding a build step that checks them.

Enforce the policy on requests by adding some middleware that rejects requests to the static folder if any uppercase characters are found in the path.

nodejs express - case sensitive URL's

From express.js api docs

case sensitive routing - Enable case sensitivity, disabled by default, treating "/Foo" and "/foo" as the same

You can change the defaults like so:

app.set('case sensitive routing', true);

NodeJs express-validator. Case sensitive checking json

I'm not sure why You need case insensitive check, cause api-s have to be strict about validation.

But I recommend You to transform common to COMMON to achieve Your goal:

router.use(
// transforming (adapting)
(req, res, next) => {
if (req.body && req.body['common']) {
req.body['COMMON'] = Object.create(req.body['common']);
delete req.body['common'];
}
next();
},

// validation
[
check('COMMON.user', 'User incorrect').not().isEmpty(),
],

// post-validation
(req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
winston.info(JSON.stringify(req.body), errors.array());
return res.status(400).json({
codigoRespuesta: -1,
textoRespuesta: "Error checking request",
errors: errors.array()
});
}
next();
},

// handler
myController.procesaRequest
);

How do you build a case insensitive REST API using Express and Node JS

If you know it's only two configurations why not try:

app.post '/api/employee', (req, res) ->
employee = new EmployeeModel({
Name: req.body.Name || req.body.name,
EmpId: req.body.EmpId || req.body.empId
})
employee.save (err) ->
if (err)
res.send err
else
res.send employee

Otherwise you would need a for...in loop as discussed here.



Related Topics



Leave a reply



Submit