0

I have tried it through MongoDB, but I can't to use JOIN Query in mongoDB and my project is wide enough. So, Want to Create Restful API in node js in MySQL. Can anyone suggest the solution

2
  • are u looking for CRUD operation in nodejs using Mysql? Commented Apr 11, 2019 at 11:42
  • Yess I've tried it and almost done with it as I've posted my answer too. You can suggest any easy way than that Commented Apr 15, 2019 at 5:38

3 Answers 3

3

For creating REST API you can go with express JS

var express = require('express');
var app = express();    

app.get('/', function (req, res) {
   //BELOW-CODE
});

You can connect Mysql by following this code:

var mysql = require('mysql');

var con = mysql.createConnection({
 host: "localhost",
 user: "yourusername",
 password: "yourpassword",
 database: "mydb"
});

con.connect(function(err) {
 if (err) throw err;
 con.query("SELECT * FROM customers", function (err, result, fields) {
 if (err) throw err;
 console.log(result);
    );
  });

Note: Install expressJS framework to get started Happy coding :-)

Sign up to request clarification or add additional context in comments.

Comments

2

For MySQL with NodeJS you can use Sequelize, it's an ORM kinda like doctrine in symfony http://docs.sequelizejs.com/

Comments

0
 `'user strict';
var sql = require('./db.js');

//Task object constructor
var Task = function(task){
    this.task = task.task;
    this.status = task.status;
    this.created_at = new Date();
};
Task.createTask = function createUser(newTask, result) {    
        sql.query("INSERT INTO tasks set ?", newTask, function (err, res) {

                if(err) {
                    console.log("error: ", err);
                    result(err, null);
                }
                else{
                    console.log(res.insertId);
                    result(null, res.insertId);
                }
            });           
};
Task.getTaskById = function createUser(taskId, result) {
        sql.query("Select task from tasks where id = ? ", taskId, function (err, res) {             
                if(err) {
                    console.log("error: ", err);
                    result(err, null);
                }
                else{
                    result(null, res);

                }
            });   
};
Task.getAllTask = function getAllTask(result) {
        sql.query("Select * from tasks", function (err, res) {

                if(err) {
                    console.log("error: ", err);
                    result(null, err);
                }
                else{
                  console.log('tasks : ', res);  

                 result(null, res);
                }
            });   
};
Task.updateById = function(id, task, result){
  sql.query("UPDATE tasks SET task = ? WHERE id = ?", [task.task, id], function (err, res) {
          if(err) {
              console.log("error: ", err);
                result(null, err);
             }
           else{   
             result(null, res);
                }
            }); 
};
Task.remove = function(id, result){
     sql.query("DELETE FROM tasks WHERE id = ?", [id], function (err, res) {

                if(err) {
                    console.log("error: ", err);
                    result(null, err);
                }
                else{

                 result(null, res);
                }
            }); 
};

module.exports= Task;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.