function addition (x, y) {
return x + y;
}
function subtraction (x, y) {
return x - y;
}
function multiplication (x, y) {
return x * y;
}
function division (x, y) {
return x / y;
}function runOperation (operationFn) {
var x = $("input.x").val();
var y = $("input.y").val();
var result = operationFn(x, y);
appendResult(result);
}
function calculate () {
var $op = $('select.op');
var operation = $op.val();
switch( operation ) {
case 'addition':
runOperation(addition);
break;
case 'subtraction':
runOperation(subtraction);
break;
// cont...
}
}
function appendResult (result) {
// cont...
}logic.js
dom.js
How would you connect these files on the frontend?
<script type="text/javascript" src="./scripts/logic.js"/>
<script type="text/javascript" src="./scripts/dom.js"/>index.html
But, with Node...
module.exports = {
addition: addition,
subtraction: subtraction,
multiplication: multiplication,
division: division
}logic.js
dom.js
var operations = require('./logic');To be clear, you won't have DOM manipulation on your server
⬜️
require('/path/to/filename')
require( )
module.exports
Resources:
Avoiding large files
Easily share common functionality
Easily share and use libraries
Functions
Objects
Strings
Arrays
Numbers
Booleans
module.exports = {
add: function(a, b) {
return a + b;
}
}
module.exports.add = function(a, b) {
return a + b;
}
exports.add = function(a, b) {
return a + b;
}Core Modules (e.g. 'http')
File Modules (e.g. '/', './', '../')
node_modules
For a list of Core Modules:
require('package-name')
It first tries to find a core module,
then tries to find a local file module,
then tries to find an installed module,
else it throws an error.
How do you save module dependencies to the package.json file that are specifically for development? Why would you do this? In general, what might fall under a development dependency?
npm install --save-dev module_name
it doesn't stand for node package manager.
"npm" is named after its command-line utility, which was organically selected to be easily typed by a right-handed programmer using a US QWERTY keyboard layout, ending with the right pinky finger in a position to type the - key for flags and other command-line arguments.
npm init
package.json
It is a module your project depends on to work.
npm install --save <module_name>
npm install --save-dev <module_name>
These dependencies are needed for development not for the application to run.
npm install
node_modules
Pro tip: Add node_modules/ to .gitignore immediately after npm install