var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "root", password: "password", database: "mydb" }); con.connect(function(err) { if (err) throw err; console.log("Connected!"); /* Create Student Table*/ var sql = "CREATE TABLE student (name VARCHAR(255), Roll_No int,address VARCHAR(255))"; con.query(sql, function (err, result) { if (err) throw err; console.log("Table created"); }); /* Insert Multiple Values*/ var sql = "INSERT INTO student (name, Roll_No,address) VALUES ?"; var values = [ ['Rahul', 1,'Bandra'], ['Ravi', 2,'Nashik'], ['Savi', 3,'Pune'], ['Seeta', 4,'Chinchwad'], ['Geeta', 5,'Wakad'], ['Sandhya', 6,'Pimpri'], ]; con.query(sql, [values], function (err, result) { if (err) throw err; console.log("Number of records inserted: " + result.affectedRows); }); /* Display student data using result object*/ con.query("SELECT * FROM student ", function (err, result, fields) { if (err) throw err; console.log(result); }); });
Write, Run & Share NodeJS code online using OneCompiler's NodeJS online compiler for free. It's one of the robust, feature-rich online compilers for NodeJS language,running on the latest LTS version NodeJS 16.14.2. Getting started with the OneCompiler's NodeJS editor is easy and fast. The editor shows sample boilerplate code when you choose language as NodeJS and start coding. You can provide the dependencies in package.json
.
Node.js is a free and open-source server environment. Node.js is very popular in recent times and a large number of companies like Microsoft, Paypal, Uber, Yahoo, General Electric and many others are using Node.js.
Google chrome's javascript engine V8
and is pretty fast.Asynchronous
, event-driven
and works on single-thread model
thus eliminating the dis-advantages of multi-thread model.Express is one of the most popular web application framework in the NodeJS echosystem.
let moment = require('moment');
console.log(moment().format('MMMM Do YYYY, h:mm:ss a'));
const _ = require("lodash");
let colors = ['blue', 'green', 'yellow', 'red'];
let firstElement = _.first(colors);
let lastElement = _.last(colors);
console.log(`First element: ${firstElement}`);
console.log(`Last element: ${lastElement}`);
Following are the libraries supported by OneCompiler's NodeJS compiler.