JavaScript - How To Create Database Using Node.js

Submitted by: 
Language: 
Visitors have accessed this post 280 times.

In this tutorial we will create a How To Create Database Using Node.js. The Node.js is a lean, fast, cross-platform JavaScript runtime environment that is useful for both servers and desktop applications. It is an open source, cross-platform runtime environment for developing server-side and networking applications. It is widely use by developers because it provide multiple libraries that simplifies the web development.

Getting started

First you will need to download & install the MySQL database server, here's the link https://www.mysql.com/downloads/.

And then download & install the Node.js, here's the link https://nodejs.org/en/.

After Node.js is installed, open the command prompt then type "npm install -g nodemon", and hit enter.
tut1

This will install nodemon, a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.

After the nodemon is installed you need to install the MySQL driver. To do that open your command prompt and cd to your working directory, then type "npm install mysql" and hit enter.
tut2

Creating the Script

This code contains the main function of the application. This code will create a privileges to access the MySQL server. To that just kindly copy and write these block of codes inside the text editor, then save it to your working directory as server.js.

  1. var mysql = require('mysql');
  2.  
  3. var con = mysql.createConnection({
  4. host: "localhost",
  5. user: "root",
  6. password: ""
  7. });
  8.  
  9. con.connect(function(err) {
  10. if (err) throw err;
  11. console.log("Connected to database!");
  12. });

After creating the script. try to run the application and see if it works.
tut3

There you have it we successfully created a How To Create Database Using Node.js. I hope that this very simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.