Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs, express routes as es6 classes

Tags:

I want to clean up my project a bit and now i try to use es6 classes for my routes. My problem is that this is always undefined.

var express = require('express'); var app = express();  class Routes {     constructor(){         this.foo = 10     }      Root(req, res, next){         res.json({foo: this.foo}); // TypeError: Cannot read property 'foo' of undefined     } }  var routes = new Routes(); app.get('/', routes.Root); app.listen(8080); 
like image 713
Michael Malura Avatar asked Nov 19 '15 08:11

Michael Malura


People also ask

Does node support es6 classes?

Finally es6 classes have landed in Node.

What is Express Router () in node JS?

js server. The express. Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests. Multiple requests can be easily differentiated with the help of the Router() function in Express.

How do I Group A route in node JS?

You can use an npm moduleYou can group your middlewares as an array and pass it to the express-inject-middleware... Show activity on this post. in express 4 to grouping your routes, you should create some changes : seperate route files in multiple files like admin and front.

Is Express used for routing?

Express uses path-to-regexp for matching the route paths; see the path-to-regexp documentation for all the possibilities in defining route paths.


2 Answers

try to use the code to pin this:

app.get('/', routes.Root.bind(routes)); 

You can get out of the boilerplate using underscore bindAll function. For example:

var _ = require('underscore');  // ..  var routes = new Routes(); _.bindAll(routes, 'Root') app.get('/', routes.Root); 

I also found that es7 allows you to write the code in a more elegant way:

class Routes {     constructor(){         this.foo = 10     }      Root = (req, res, next) => {         res.json({foo: this.foo});     } }  var routes = new Routes(); app.get('/', routes.Root); 
like image 162
zag2art Avatar answered Sep 18 '22 06:09

zag2art


This is happening because you've passed a method as a standalone function to express. Express doesn't know anything about the class that it comes from, therefore it doesn't know which value to use as this when your method is called.

You can force the value of this with bind.

app.get('/', routes.Root.bind(routes)); 

Or you can use an alternative construct for managing routes. You can still make use of a lot of the syntactic benefits for object oriented programming without classes.

function Routes() {   const foo = 10;    return {     Root(req, res, next) {       res.json({ foo });     }   }; }  const routes = Routes(); app.get('/', routes.Root); app.listen(8080); 
  • You won't have to worry about the value of this
  • It doesn't matter whether the function is called with new or not
  • You can avoid the complexity of calling bind on each route

There's a good list of resources here, on why ES6 classes are not as good as they might seem.

like image 38
Dan Prince Avatar answered Sep 19 '22 06:09

Dan Prince