Skip to content

Instantly share code, notes, and snippets.

View picpoint's full-sized avatar
:octocat:
<html> .less {js} <?php?> "mysql"

rmtar picpoint

:octocat:
<html> .less {js} <?php?> "mysql"
View GitHub Profile
@picpoint
picpoint / serverjson.js
Created May 15, 2019 19:10
simple server for work width json files
const http = require('http');
const todos = require('./data/todos.json');
const server = http.createServer(function (req, res) {
if (res.statusCode > 200) {
throw new Error('SERVER NOT STARTED', res.statusCode);
}
if (req.url == '/todos') {
res.writeHead(200, {'Content-Type': 'application/json'});
@picpoint
picpoint / index.html
Last active May 14, 2019 19:37
server to work with files
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
@picpoint
picpoint / app.js
Created May 13, 2019 20:50
consolle app to github
const github = require('./github');
const username = process.argv[2];
github.getRepos(username, function (err, repos) {
if (err) {
throw new Error(`ERROR HAS OCCURED... ${err.message}`);
} else {
repos.forEach(function (repo) {
console.log(repo.name);
});
@picpoint
picpoint / minify
Last active May 13, 2019 18:21
Simple minify to files
const fs = require('fs'); // подключаем модуль файловой системы
const output = fs.createWriteStream('script.min.js'); // в константу записывает исходящий поток, которая создаёт файл
fs.readFile('script.js', 'utf-8', function (err, data) { // читаем файл
if (err) {
throw new Error('ERROR TO READ FILE...'); // если ошибка, выбрасываем исключение
} else {
for (let i = 0; i < data.length; i++) { // пробегаемся по всем символам
if (data[i] != ' ' && data[i] != '' && data[i] != '\n' && data[i] != '\t') { // если не '', ' ', \n, \t
output.write(data[i]); // записываем каждый символ в файл
@picpoint
picpoint / pipe
Created May 12, 2019 20:03
simple read/write stream
const fs = require('fs'); // подключаем файловую систему
const input = fs.createReadStream('lorem.txt'); // создаём поток чтения
const output = fs.createWriteStream('newlorem.txt'); // создаём поток записи
input.pipe(output); // из чтения передаём данные в запись и создаём новый файл
const fs = require('fs');
const streamInput = fs.createReadStream('ent.mp4'); // входящий поток
const streamOutput = fs.createWriteStream('new.mp4'); // исходящий поток
streamInput.on('data', function (part) { // событие на входящем потоке
streamOutput.write(part); // записывает части данных в файл new.mp4
});
@picpoint
picpoint / streams. Read file
Last active May 12, 2019 15:19
Stream to read/write files
const fs = require('fs');
const stream = fs.createReadStream('natural.txt', 'utf-8'); // создаём поток чтения
let records = '';
stream.on('data', function (part) { // вешаем событие с данными и по частям дописываем в переменную, данные которые пришли
console.log(records += part);
});
@picpoint
picpoint / tick EventEmiter
Last active May 12, 2019 15:20
Events EventEmiter
// Н Ё Х
const EventEmiter = require('events');
class Timer extends EventEmiter {
constructor(total) {
super();
this.total = total;
this.ticks = 0;
}
@picpoint
picpoint / events
Last active May 12, 2019 15:16
module Events
const EventEmiter = require('events'); // подключаем модуль
const myEE = new EventEmiter();
myEE.on('ready', function () { // создаём событие ready
console.log('is ready ...');
});
myEE.on('start', function () { // создаём событие start
console.log('is start ...');
});
@picpoint
picpoint / fs
Last active May 7, 2019 09:48
task 3 to fs
//Файловая система
/*
+++Задание 1
Реализовать программу, которая синхронно читает файл и выводит количество строк(\n) содержащихся в файле в консоль
*/
/*
const fs = require('fs');
var file = fs.readFileSync('test.txt', 'utf-8');
file = file.split('\n');