Please Kindly Share To Your Social Media
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
"description": "REST CRUD Example Node.js & 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.
ubuntu@AcerXtimeline:~/rest-crud$ node server.js
Fire your browser
You might need to pay attention on this , as we're using REST
|/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.
The Source code will output this :