0

When I added a linter to my code, I noticed I got a lot of errors regarding 'use strict'; so I added the line to a lot of files. However this broke some code in my main app.js file, which looks like this:

'use strict';

var express = require('express');
var timeout = require('connect-timeout');
//var logger = require('morgan');
var requireDir = require('require-dir');
var app = express();

// Load all the routes in the routes file into app middleware
// This assumes that the route exports an express.Router() object
var routes = requireDir('./routes');
for (var i in routes) app.use('/', routes[i]);


// Set the timeout
app.use(timeout('5s'));

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = 'ERROR: Invalid request to the server';
  err.status = 404;
  next(err);
});

if (!module.parent) {
  app.listen(3000);
  console.log('engine-map-service opend on port 3000');
}

Which returns the following error stack:

  TypeError: Cannot assign to read only property 'status' of ERROR: Invalid request to the server
     at /usr/src/app/app.js:21:14
     at Layer.handle [as handle_request] (/usr/src/app/node_modules/express/lib/router/layer.js:95:5)
     at trim_prefix (/usr/src/app/node_modules/express/lib/router/index.js:312:13)
     at /usr/src/app/node_modules/express/lib/router/index.js:280:7
     at Function.process_params (/usr/src/app/node_modules/express/lib/router/index.js:330:12)
     at next (/usr/src/app/node_modules/express/lib/router/index.js:271:10)
     at /usr/src/app/node_modules/connect-timeout/index.js:64:5
     at Layer.handle [as handle_request] (/usr/src/app/node_modules/express/lib/router/layer.js:95:5)
     at trim_prefix (/usr/src/app/node_modules/express/lib/router/index.js:312:13)
     at /usr/src/app/node_modules/express/lib/router/index.js:280:7 

What exactly is use strict? I have read a few articles and I haven't seen anything going in depth about how I might need to change my behavior as a developer, and when I should or shouldn't use it. I found more than one article which recommended starting every js file with use strict but clearly there are cases which I am missing.

eignhpants
  • 1,611
  • 4
  • 26
  • 49
  • possible duplicate of [What does "use strict" do in JavaScript, and what is the reasoning behind it?](http://stackoverflow.com/questions/1335851/what-does-use-strict-do-in-javascript-and-what-is-the-reasoning-behind-it) – jmoerdyk Sep 29 '15 at 15:52
  • 1
    As to the immediate error, it does no good to add a property to a primitive string since it'll never be retrievable. Any code that tries to read the `status` property will just get `undefined`. So strict mode is letting you know this. –  Sep 29 '15 at 15:55
  • Congratulations. You've just discovered the benefits of strict mode. It finds some types of errors for you a parse time rather than only later at run time - thus preventing you from shooting yourself in the foot. It turns some bad coding practices that were previously permitted by the JS parser into errors. – jfriend00 Sep 29 '15 at 16:34

1 Answers1

3

The use of use strict is covered extensively in What does "use strict" do in JavaScript, and what is the reasoning behind it?.

In your specific case, it is catching a TypeError that wouldn't have otherwise been thrown until a certain condition occurred at runtime.

In your error handler you are creating a string called err and then attempting to assign a property to it. You cannot assign properties to strings.

If you want to create an error object that contains both a message and a status, you could do something like this:

app.use(function(req, res, next) {
  var err = {
    message: 'ERROR: Invalid request to the server',
    status: 404
  };
  next(err);
});
Community
  • 1
  • 1
duncanhall
  • 11,035
  • 5
  • 54
  • 86