Node.js provides multiple ways for the propagating and handling errors occur during the Node.js application execution. The error reported by a Node.js application depends on the type of error. There are generally four types of errors in the Node.js application. Standard JavaScript errors – Like EvalError SyntaxError RangeError ReferenceError TypeError URIError System Error – For example attempt to open a file that does not exist, attempting to send data over a closed socket, etc; User-specified Errors – These errors triggered by application code. Also can be known as human mistakes…
Read MoreTag: nodejs
Node.js – Hello World Example
You can run Node.js application an console based or web-based application. Console based application will run your system terminal and a web-based application will use an inbuilt web server to make an application accessible on the web browser. Console-based Hello World Example Use the Node.js console module to print output on your system console. Create a JavaScript file nodejs_hello_console.js using the below content.
1 | console.log('Hello World'); |
Execute your script using the node node nodejs_hello_console.js [output] Hello World! Web-based Hello World Example A Node.js web application is build with 3 parts. Import module…
Read MoreNode.js – MySQL Connection
Use mysql module available on npmjs repository to connect MySQL database and execute queries from Node.js application. Install Node.js MySQL Driver You must have MySQL module installed in your application. You can install it using the following command. npm install mysql Node.js Connect to MySQL Create a JavaScript file in your application and edit it in your favorite text editor. vim connect_mysql.js Add the following content. Change the MySQL connection values as per your system setup.
1 2 3 4 5 6 7 8 9 10 11 12 | var mysql = require('mysql'); var dbconn = mysql.createConnection({ host: "localhost", port: "3306", user: "root", password: "secure_password" }); dbconn.connect(function(err) { if (err) throw err; console.log("Database connected successfully!"); }); |
Test Connection Finally, execute the connect_mysql.js file using node. On successful connection, it…
Read MoreNode.js – REPL
REPL stands for Read, Eval, Print, and Loop. Node.js also provides a REPL environment for quicky testing and debugging Node.js code. You can also store all the commands executed on Node REPL by setting the NODE_REPL_HISTORY environment variable. Node.js REPL will store all commands to the file set with this variable export NODE_REPL_HISTORY=/var/log/node-repl.log Use CTRL + C + C to exit from REPL console. Start REPL Console Run command node on your system console and this will provide you REPL prompt (>) for Node.js. Where you can run and test…
Read More