Please Kindly Share To Your Social Media
Recommended : New Node.js Example project with Hapi.js. 

I've created a Simple CRUD Node.js & MySQL and its live demo. But it was a regular CRUD and a lil bit outdated Express version (version 3). Now we'll be making the new one, still a nodejs mysql CRUD but we add some functionality (RESTful, ajax, and Input validator ).

Before we get started, I assume that you have installed NPM in your machine .  I am using Ubuntu 14.04 here ( there should be no issue if you're using different OS) . 

Now create a folder somewhere in your machine (have it your way about the name) , but im gonna name it 'rest-crud' . Go inside the folder and create a file named package.json or in your terminal you can just do this : 

ubuntu@AcerXtimeline:~/rest-crud$ sudo touch package.json

Copy-paste this into package.json

{
  "name": "rest-crud",
  "description": "REST CRUD Example Node.js & Mysql",
  "version": "0.0.1",
  "private": true,
  "main": "server.js",
  "dependencies": {
    "body-parser": "1.9.0",
    "ejs": "^1.0.0",
    "express": "4.9.5",
    "express-myconnection": "*",
    "express-validator": "^2.6.0",
    "mysql": "*"
  }
}

Those are what we need before we start coding. With that config, we'll be having newest Express, ejs and other depedencies (CMIIW). 

Right after that, run this command to intall them all

ubuntu@AcerXtimeline:~/rest-crud$ npm install


 hmmmmm..... lets just skip this anyway. 


You can just Fork/Download the Source here , install/clone it to your local. Create a DB named test and import the sql file given.

 run it 

ubuntu@AcerXtimeline:~/rest-crud$ node server.js

Fire your browser 

localhost:3000/api/user

You might need to pay attention on this , as we're using REST 

Route HTTP verb Desc
/api/user GET get All users
/api/user POST add new user
/api/user/:user_id GET get a user (for editing)
/api/user/:user_id PUT update a user
/api/user/:user_id DELETE delete a user

I use jQuery Ajax for DELETE, PUT and POST (see the source code)  . And also you will find in the Source Code (server.js) I put the routes and code all inside it. Its highly recommended  we separate them. Check a simple example here how to do that.



image host