Links
El meu terminator:
const terminator = function() {
//let exec = require("child_process").exec;
const util = require('util');
const exec = util.promisify(require('child_process').exec);
process.stdin.resume();
function handle(signal) {
console.warn(` *** process node *** `);
async function cmd() {
const { stdout, stderr } = await exec("ps -ef | grep node | grep -v grep | awk '{print $1, $2, $3, $8 }'");
console.warn(`UID PID PPID CMD`);
if (stdout) { console.warn(` ${ stdout } \n `) };
if (stderr) { console.warn(` ${ stderr } \n `) };
console.log(`Signal rebuda : ${signal}. El process ${ process.pid } es cancel.la \n`);
console.log(`Directori actual: ${process.cwd()}\n`);
console.log(`${MyFramework.Utils.Imprimir.impObjecte("Impresió process.env", process.env)} `);
process.exit(0);
}
cmd();
}
process.on('SIGINT', handle);
process.on('SIGTERM', handle);
process.on('uncaughtException', handle); // tinc dubtes d'aquesta senyal, em sembla que s´ha de sortir amb exit(1) perque continui: consultar llibre 'Patterns nodejs'
};
El meu node-fetch/node-axios
const fetch = require("node-fetch"); //
const axios = require("axios");
// accedim a una url per el módul 'node-fetch'
const getPerFetch = async(url) => {
try {
const resultat = await fetch(url);
return resultat.json();
} catch (err) {
console.log("Error! Catch getPerFetch", err)
new Error('Error! Catch getPerFetch' + err)
}
}
// accedim a una url per el módul 'axios'
const getPerAxios = async(url) => {
try {
const resultat = await axios.get(url);
return resultat.data
} catch (err) {
new Error('Error! Catch getPerAxios' + err.response.status)
}
}
// ************************************************************
// nodeFetch: Llegim una url de usuaris amb el modul node-fetch
// **********************************************************
function nodeFetch(response, pathname, postData, _extname) {
console.log(` Manipulador de la peticio '/nodeFetch' : ${ pathname } ha estat cridat. `);
(async(url) => {
try {
const contentFile = await getPerFetch(url);
response.writeHead(200, { 'Content-Type': 'text/html; charset=UTF-8' });
response.write(montarHtmlUsers(contentFile));
response.end();
} catch (error) {
console.log("//////////////////// APP PROBLEMS", error);
response.writeHead(404, { 'Content-Type': 'text/html; charset=UTF-8' });
response.write(`Error ${contentFile} inexistent
`);
response.end();
}
})('https://jsonplaceholder.typicode.com/users')
}
// ************************************************************
// nodeAxios: Llegim una url de usuaris amb el modul axios
// **********************************************************
function nodeAxios(response, pathname, postData, _extname) {
console.log(` Manipulador de la peticio '/nodeAxios' : ${ pathname } ha estat cridat. `);
(async(url) => {
try {
const contentFile = await getPerAxios(url);
response.writeHead(200, { 'Content-Type': 'text/html; charset=UTF-8' });
response.write(montarHtmlUsers(contentFile));
response.end();
} catch (error) {
console.log("//////////////////// APP PROBLEMS", error);
response.writeHead(404, { 'Content-Type': 'text/html; charset=UTF-8' });
response.write(`Error ${contentFile} inexistent
`);
response.end();
}
})('https://jsonplaceholder.typicode.com/users')
}