Javascript on the Server
"Any application that can be written in Javascript, will eventually be written in Javascript."
--Jeff Atwood, 2007
package manager by far in all the history of package managers
Event-driven programming is a programming paradigm in which the flow of the program is determined by events such as
If node is single-threaded, you might be wondering how to take advantage of multiple core architectures.
Scalability in Node is not an afterthought. It’s something that’s baked into the core of the runtime.
Node is named Node to emphasize the idea that a Node application should comprise multiple small distributed nodes that communicate with each other.
separating by data (e.g. by region)
using node's built-in cluster module.
separating by function
If you are lucky enough that your app needs to scale, you may eventually need all three:
const fs = require('fs')
// blocks here until file is read
const data = fs.readFileSync('/file.md')
const fs = require('fs')
fs.readFile('/file.md', (err, data) => {
if (err) throw err
})
const util = require('util')
const fs = require('fs')
const readFile
= util.promisify(fs.readFile)
readFile('/file.md')
.then((data) => {
// Do something with `data`
})
.catch((error) => {
// Handle the error.
})
via callback (traditional)
via promise (new)
module.exports = {
myModule: 'hello world'
}
export const myModule = 'hello world'
my-module.js
my-module.mjs
const myModule
= require('./my-module').myModule
console.log(myModule)
parent.js
import { myModule } from './my-module'
console.log(myModule)
parent.mjs
module.exports = leftpad;
function leftpad (str, len, ch) {
str = String(str);
var i = -1;
if (!ch && ch !== 0) ch = ' ';
len = len - str.length;
while (++i < len) {
str = ch + str;
}
return str;
}
NPM makes it easy to both consume and produce TINY packages. And that can potentially "break the internet"
Code reuse and reducing
cognitive load for developers is a very good thing.
However, there are some caveats: