简体   繁体   中英

Confused about 'use strict' in JavaScript/nodejs

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.

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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM