<form enctype="application/x-www-form-urlencoded" method="POST" action="/submit">
<input type="text" name="name" value="Вася"><br>
<input type="text" name="surname" value="Пупкин"><br>
<input type="submit" value="Send">
</form>
if(req.url == '/submit' && req.method == 'POST') {
req.on('data', function(data){
var s = decodeURIComponent(data.toString());
console.log(s);
var obj = {};
var arr = s.split('&');
arr.forEach( el => {
var a = el.split('=')
obj[a[0]] = a[1];
});
console.log(obj);
});
return res.end('POST');
}
name=Вася&surname=Пупкин
{ name: 'Вася', surname: 'Пупкин' }
var locals = res.locals;
locals.title = 'My Blog';
const pathAPI = '/api/blog';
const requestOptions = {
url: apiOptions.server + pathAPI,
method: 'GET',
json: {}
};
request(requestOptions, function (error, response, body) {
if (error) {
console.log(error);
}
locals.texts = body;
// дальше смело второй запрос за аватарку и в его колбеке
request(....
...
locals.avatar = body
res.render('pages/blog');
});
npm install -g forever
apt-get install nodejs-legacy
forever start server.js
setInterval(function(){
getData().then( data => {
console.log(data);
});
}, 3000);
async function getData() {
var [h, t] = await new Promise( resolve => {
sensor.read(11, 6, function(err, temperature, humidity) {
resolve( [humidity.toFixed(1), temperature.toFixed(1)] );
});
});
var cpu = await new Promise( resolve => {
os.cpuUsage(function(v){
resolve(v*100);
});
});
var cpu2 = await new Promise( resolve => {
os.cpuFree(function(v){
resolve(v*100);
});
});
var totalmem = os.totalmem();
var freemem = os.freemem();
var ret = {
freemem: freemem,
totalmem: totalmem,
temp: t,
hum: h,
cpu: cpu,
cpu2: cpu2,
};
return JSON.stringify(ret);
}