I am learning ES 6 syntax of classes. I come from a C# background, so I apologize if my terminology isn't correct. Or, if I'm doing something that looks odd.
I'm building a web app as a learning exercise. It's built on Node and Express. I have some routes defined like this:
'use strict';
module.exports = function() {
const app = this;
app.use('/blog', function(req, res) {
console.log('loading blog postings');
res.render('blog', {});
});
app.use('/', function(req, res) {
console.log('looking up: ' + req.path);
res.render('home', {});
});
};
I'm trying to put some viewModels behind these views. So, I have a directory called viewModels
. That directory has these files:
index.js
blog.js
home.js
The files currently, probably inaccurately, look like this:
index.js
'use strict';
module.exports = function() {
const HomeViewModel = require('./home);
const BlogViewModel = require('./blog);
};
blog.js
export default class BlogViewModel {
constructor() {
this.title = 'My Blog';
}
}
home.js
export default class HomeViewModel {
constructor() {
this.title = 'Home';
}
}
My thought, was that I could use index.js
as a way to define my package or namespace. Then, in my routing code, I could do something like this:
'use strict';
module.exports = function() {
const app = this;
const ViewModels = require('../viewModels/index');
app.use('/blog', function(req, res) {
console.log('loading blog postings');
let viewModel = new ViewModels.BlogViewModel();
res.render('blog', viewModel);
});
app.use('/', function(req, res) {
console.log('looking up: ' + req.path);
let viewModel = new ViewModels.HomeViewModel();
res.render('home', viewModel);
});
};
However, when I attempt this, I get some runtime errors that say "Error: Cannot find module '../viewModels/index'". This implies that I'm not setting up my module properly. But, it looks like I am what am I doing wrong?