Node.js - astromechanic/cheat_sheets GitHub Wiki
Способы включить модуль в код:
1. File
const utils = require('./utils.js');
2. Node API:
const fs = require('fs');
3. npm module:
const chalk = require('chalk);
Экспорт из файла:
const name = 'Alice';
module.exports = name;
Write to files and append:
fs.writeFileSync('notes.txt', 'Hi there!\n');
fs.appendFileSync('notes.txt', 'This is my Node.js Project');
Work with npm
npm init // create package.json
npm install validator // install validator package
Get input from user: process.argv
Это массив, нулевой элемент которого - путь, где установлен node, первый - путь, где лежит наш код, 2+ - строки, разделенные по пробелу, которые ввел юзер
Хороший пример, как сделать команду для приложения:
node app.js add --title="First note" --body="Hello there! This is my first note"
Есть npm пакет, который парсит инпут: npm install yargs
Add command:
yargs.command({
command: 'remove',
describe: 'Delete a note',
handler: function() {
console.log('Deleting a note');
}
});
yargs.parse();