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:
- macOX is case-insensitive file system by default
- even you have only one file
extendableError.js
, but yourequire
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
Linux Find Out Hyper-Threaded Core Id
Extract List of File Names in a Zip Archive When 'Unzip -L'
What Is This $Path in Linux and How to Modify It
Grep String Inside Double Quotes
Git- How to Kill Ssh-Agent Properly on Linux
What Does "< /Dev/Null >& /Dev/Null" at the End of a Command Do
Compiling a Linux Program for Arm Architecture - Running on a Host Os
How to Disable Editing My History in Bash
How to Know Which Device Is Connected in Which /Dev/Ttyusb Port
Cannot Clone Repository: Fatal: R Any Gitolite-Admin Gitolite Denied by Fallthru
Let Non-Root User Write to Linux Host in Docker
I Cannot Get a Result from a Single Line Put into the Erlang Shell
How to Make Perl Wait for Child Processes Started in the Background with System()
Cannot Install Extensions in Visual Studio Code
On Linux, How to Make Sure to Unlock a Mutex Which Was Locked in a Thread That Dies/Terminates