In Node.js, how to import functions from another JavaScript file like source common.sh in Bash? In the .js file to be imported such as ‘common.js’, organize functions to be exported like module.exports = { func1: function () { // func1 impl }, func2: function () { // func2 impl } }; In the other .js
Read more
Tag: node.js
How to get file extension in JavaScript?
Posted onHow to get file extension from a file name in JavaScript? For examples, For “file.txt”, I want to get “txt”. For “file2.multi.ext.fileext”, I want to get “fileext”. This JavaScript function works well for me. function getExt(filename) { var idx = filename.lastIndexOf(‘.’); // handle cases like, .htaccess, filename return (idx < 1) ? “” : filename.substr(idx
Read more
How to convert an object to json in Node.js?
Posted onHow to convert general JavaScript objects to json in Node.js? Assume jo is a JavaScript object, you can convert it to a json string by: JSON.stringify(jo)
How to parse POST data in node.js?
Posted onSome JavaScript libraries send data by POST instead of JSON. In node.js, how to parse the POST data? You can use the “querystring” module: var qs = require(‘querystring’) The following piece of code works well for me. The comments should help you read the code well. var post_handle = function(request, response) { if (request.method ==
Read more
How to exit the program in Node.js?
Posted onWhen fatal error happened, we’d like to kill the program. How to exit the program in Node.js? You can use: process.exit() Here, “process” is a global process object. exit method ends the process. process.exit([code]) Ends the process with the specified code. If omitted, exit uses the ‘success’ code 0. To exit with a ‘failure’ code:
Read more
How to install gitbook?
Posted onHow to install gitbook on my own Linux box? First, install node.js following https://www.systutorials.com/qa/1268/how-to-install-node-js-on-fedora or How to install node.js on Ubuntu/Linux Mint depending on your distro. Second, install gitbook by npm to /opt/: # cd /opt/ # npm install gitbook Then, the gitbook can be invoked by /opt/node_modules/gitbook/bin/gitbook.js You may need to install the latest
Read more
How to install node.js on Ubuntu/Linux Mint?
Posted onHow to install node.js on Ubuntu/Linux Mint? This is how I install node.js on Linux Mint: # aptitude install nodejs nodejs-legacy npm The nodejs-legacy makes sure the command node will invoke node.js.
How to install node.js on Fedora?
Posted onHow to install node.js on Fedora? You may install it by: # yum install nodejs npm