how to exit terminal from a Node.js program All In One
how to exit terminal from a Node.js program All In One
exit(0)
&process.exit(0)
// commonjs module using `require` keyword
const { exit } = require("process");
exit(0);
// const process= require("process");
// process.exit(0);
https://nodejs.org/api/process.html#processkillpid-signal
https://nodejs.org/api/process.html#event-exit
https://nodejs.org/api/process.html#exit-codes
https://nodejs.dev/learn/how-to-exit-from-a-nodejs-program
//CJS
const process = require('process');
process.on('SIGHUP', () => {
console.log('Got SIGHUP signal.');
});
setTimeout(() => {
console.log('Exiting.');
process.exit(0);
}, 100);
process.kill(process.pid, 'SIGHUP');
// ESM
import process, { kill } from 'process';
process.on('SIGHUP', () => {
console.log('Got SIGHUP signal.');
});
setTimeout(() => {
console.log('Exiting.');
process.exit(0);
}, 100);
kill(process.pid, 'SIGHUP');
demo
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-04-01
* @modified
*
* @description Node.js pdf crawler
* @augments
* @example
* @link
*
*/
// 1. commonjs module using `require` keyword
const fs = require("fs");
const path = require("path");
const { exit } = require("process");
// const process= require("process");
// process.exit(0);
const request = require("request-promise-native");
const log = console.log;
// 2. custom download folder
const folder = path.resolve(__dirname, '../pdf');
// log('folder', folder);
// 3. check if the folder exists, if not create it
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder);
}
async function downloadPDF(url, filename) {
log('🚧 pdf downloading ...');
const pdfBuffer = await request.get({
uri: url,
encoding: null,
// encoding: 'utf-8',
});
// 4. write file to local file system
fs.writeFileSync(filename, pdfBuffer);
log('✅ pdf download finished!');
// 5. exit the terminal after download finished
exit(0);
}
const url = 'https://cs193p.sites.stanford.edu/sites/g/files/sbiybj16636/files/media/file/l1.pdf';
const filename = folder + '/cs193p-2021-l1.pdf';
// log('filename =', filename);
downloadPDF(url, filename);
https://stackoverflow.com/a/71764490/5934465
refs
node.js pdf download crawler
https://www.npmjs.com/package/pdf-crawler
https://www.cnblogs.com/xgqfrms/p/14249005.html
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16107662.html
未经授权禁止转载,违者必究!