What am I doing wrong? Result of module method getFile is always undefined.
Here is code (routing):
let router = {
paths : {
'rest' : 'rest.info',
'data' : 'data-info'
},
getFile : (url) => {
console.log(this.paths);
}
};
module.exports = {
router : router
}
Here is code from simple server:
const http = require('http');
const fs = require('fs');
const router = require('./routing');
http.createServer((req, res) => {
router.getFile('rest');
res.writeHead(200, {'Content-Type': 'text/html'});
fs.createReadStream('./views/myview.html').pipe(res);
}).listen(3000);
Try const router = require('./routing').router
Because the module.exports is an object with router
as a property
This can also be accomplished through destructuring using a suitable node version:
const { router } = require('./routing');
I believe you just need to pluralize export
like so:
module.exports = {..
Hope this helps!
In order to access this.paths
change your getFile method like this:
getFile(url) {
console.log(this.paths);
}
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.