javascript - In Node.js, how do I "include" functions from my other files? -


let's have file called app.js. pretty simple:

var express = require('express'); var app = express.createserver(); app.set('views', __dirname + '/views'); app.set('view engine', 'ejs'); app.get('/', function(req, res){   res.render('index', {locals: {     title: 'nowjs + express example'   }}); });  app.listen(8080); 

what if have functions inside "tools.js". how import them use in apps.js?

or...am supposed turn "tools" module, , require it? << seems hard, rather basic import of tools.js file.

you can require js file, need declare want expose.

// tools.js // ======== module.exports = {   foo: function () {     // whatever   },   bar: function () {     // whatever   } };  var zemba = function () { } 

and in app file:

// app.js // ====== var tools = require('./tools'); console.log(typeof tools.foo); // => 'function' console.log(typeof tools.bar); // => 'function' console.log(typeof tools.zemba); // => undefined 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -