A distributed version control system
npm install <package>
)npm publish
)Provides global module
, exports
and require()
to define this files API
// module1.js
exports.hello = 'World';
// or
module.exports = {
hello: 'World'
}
Using the module
// main.js
var mod1 = require('./module1');
console.log(mod1.hello); // -> World
CommonJS specification for describing JavaScript packages
{
"name": "nodeschool",
"version": "0.1.0",
"author": "Nodeschool <people@nodeschool.com>",
"description": "NodeJS FTW!",
"scripts": {
"test": "mocha test",
"start": "node lib/start.js"
},
"main": "./lib/main.js",
"repository": {
"type": "git", "url": "https://github.com/yycjs/node-up"
},
"dependencies": { "somePackage": "^1.0.0" },
"devDependencies": { "some-dev-only-package": "^0.2.0" },
"license": "MIT"
}
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Split functionality into contained units. Ideally each function should perform one unit of work.