const socket = io('http://localhost:3000');
socket.on('progress', function (data) {
// сколько % загружено на сервер
const progress = Math.round(data.bytesReceived / data.bytesExpected * 100)
progressBar.style.width = progress + '%' // CSS ширина прогресс бара
});
const formidable = require('formidable')
const form = new formidable.IncomingForm();
.....
// событие progress у загружаемой формы
form.on('progress', function (bytesReceived, bytesExpected) {
var progress = {
type: 'progress',
bytesReceived: bytesReceived,
bytesExpected: bytesExpected
};
// шлем событие progress в браузер
req.io.sockets.emit('progress', progress);
});
Возможно сделать счетчик скачиваний (кликов) по ссылке на js? Чтобы записывался в куки на 5 дней.
при загрузке страницы разберёт значения фильтров из url
params = new URLSearchParams(
new URL('http://domain.com/filter?size=M&color=1&color=2&manufacturer=aaa&manufacturer=ddd').search
)
console.log([...params])
/*
(5) [Array(2), Array(2), Array(2), Array(2), Array(2)]
0: (2) ["size", "M"]
1: (2) ["color", "1"]
2: (2) ["color", "2"]
3: (2) ["manufacturer", "aaa"]
4: (2) ["manufacturer", "ddd"]
*/
arr[0].map(n => arr[1].map(nn => nn * n))
result = []
for (let n of arr[0]) {
const subArr = []
for (let nn of arr[1]) {
subArr.push(n * nn)
}
result.push(subArr)
}
// и не так
{
"myfunc": function() { //My code }
}
// а так
{
_id: "myfunc",
value : function(x) { return x; }
}
const bodyParser = require("body-parser");
const app = express()
// надо же подключить к аппу
// Body parser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
song.replace(/(WUB)+/g," ").trim()
data = [
{x: "100", y: "40", z: "first"}, {x: "120", y: "10", z: "second"},
{x: "0", y: "100", z: "second"}, {x: "300", y: "150", z: "third"}
]
filter = {x: {max: "100", min: "20"}, y: {max: "100", min: "20"}, z: ["first", "second"]}
isBetween = (num, bounds) => +num >= +bounds.min && +num <= +bounds.max
data.filter(obj => isBetween(obj.x, filter.x) && isBetween(obj.y, filter.y) && filter.z.includes(obj.z))
// 0: {x: "100", y: "40", z: "first"}
async function getData() {
const urls = {
comments: 'https://jsonplaceholder.typicode.com/comments',
users: 'https://jsonplaceholder.typicode.com/users',
posts: 'https://jsonplaceholder.typicode.com/posts'
}
const data = {}
for (const [key, value] of Object.entries(urls)) {
const res = await fetch(value)
data[key] = await res.json()
}
return data
}
console.log(await getData())
window.addEventListener('scroll', onScroll)
function onScroll(e) {
if (pageYOffset > 1000) {
console.log('BOOOOOM')
/* do stuff */
window.removeEventListener('scroll', onScroll)
}
}