国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

ExampleofCRUDwithNode.js&MySQL_MySQL

來源:懂視網 責編:小采 時間:2020-11-09 19:14:03
文檔

ExampleofCRUDwithNode.js&MySQL_MySQL

ExampleofCRUDwithNode.js&MySQL_MySQL:NodeJS This time I'd like to share a basic and simple example of CRUD Operation in Node.js and MySQL. Its a lil bit hard to find tutorial Node.js n MySQL as poeple tend to use Mongoose instead of MySQL. Before we start, Please mind the env
推薦度:
導讀ExampleofCRUDwithNode.js&MySQL_MySQL:NodeJS This time I'd like to share a basic and simple example of CRUD Operation in Node.js and MySQL. Its a lil bit hard to find tutorial Node.js n MySQL as poeple tend to use Mongoose instead of MySQL. Before we start, Please mind the env
NodeJS

This time I'd like to share a basic and simple example of CRUD Operation in Node.js and MySQL. Its a lil bit hard to find tutorial Node.js n MySQL as poeple tend to use Mongoose instead of MySQL.

Before we start, Please mind the environment of this Application.

  • I'm using Ubuntu
  • NPM, Express
  • MySQL for Node
  • I haven't tested it yet on Windows. but i bet this will work too.

    Installing all those things above

    Install Node.js, NPM and Express in Ubuntu

    After you installation's completed, lets start creating your project folder:

    ubuntu@AcerXtimeline:~$ express hello_world

    once your hello_world folder is ready, Install MySQL. Go inside hello_world

    ubuntu@AcerXtimeline:~/hello_world$ npm install mysql

    We need a connection manager in Express. install it

    ubuntu@AcerXtimeline:~/hello_world$ npm install express-myconnection

    Now take a look at this Folder structure

    See you folder structure, compare it to the picture above.make new folder/files that you dont have yet in folder just like on the pic.

    Are we ready yet ?

    1. Careate a MySQL Database :nodejs and create a tablecustomer (id,name,address,email,phone). or you can import the SQL in source code (see the end of this tuts)

    2. Open app.js . by default some codes are already given for you. we'll just need to add a lil more codes.

    /**
    * Module dependencies.
    */
    var express = require('express');
    var routes = require('./routes');
    var http = require('http');
    var path = require('path');
    //load customers route
    var customers = require('./routes/customers');
    var app = express();
    var connection= require('express-myconnection');
    var mysql = require('mysql');
    // all environments
    app.set('port', process.env.PORT || 4300);
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'ejs');
    //app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.json());
    app.use(express.urlencoded());
    app.use(express.methodOverride());
    app.use(express.static(path.join(__dirname, 'public')));
    // development only
    if ('development' == app.get('env')) {
    app.use(express.errorHandler());
    }
    /*------------------------------------------
    connection peer, register as middleware
    type koneksi : single,pool and request
    -------------------------------------------*/
    app.use(

    connection(mysql,{

    host: 'localhost',
    user: 'root',
    password : '',
    port : 3306, //port mysql
    database:'nodejs'
    },'request')
    );//route index, hello world
    app.get('/', routes.index);//route customer list
    app.get('/customers', customers.list);//route add customer, get n post
    app.get('/customers/add', customers.add);
    app.post('/customers/add', customers.save);//route delete customer
    app.get('/customers/delete/:id', customers.delete_customer);//edit customer route , get n post
    app.get('/customers/edit/:id', customers.edit);
    app.post('/customers/edit/:id',customers.save_edit);
    app.use(app.router);
    http.createServer(app).listen(app.get('port'), function(){
    console.log('Express server listening on port ' + app.get('port'));
    });

    remember to make new files/folder like shown on the above pic.
    Now, wee need codes to DO THE CRUD. the file's locatedroutes/customers.js

    /*
    * GET customers listing.
    */
    exports.list = function(req, res){
    req.getConnection(function(err,connection){

    connection.query('SELECT * FROM customer',function(err,rows) {

    if(err)
    console.log("Error Selecting : %s ",err );

    res.render('customers',{page_title:"Customers - Node.js",data:rows});

    });

    });

    };
    exports.add = function(req, res){
    res.render('add_customer',{page_title:"Add Customers-Node.js"});
    };
    exports.edit = function(req, res){

    var id = req.params.id;

    req.getConnection(function(err,connection){

    connection.query('SELECT * FROM customer WHERE id = ?',[id],function(err,rows)
    {

    if(err)
    console.log("Error Selecting : %s ",err );

    res.render('edit_customer',{page_title:"Edit Customers - Node.js",data:rows});

    });

    });
    };
    /*Save the customer*/
    exports.save = function(req,res){

    var input = JSON.parse(JSON.stringify(req.body));

    req.getConnection(function (err, connection) {

    var data = {

    name: input.name,
    address : input.address,
    email : input.email,
    phone : input.phone

    };

    var query = connection.query("INSERT INTO customer set ? ",data, function(err, rows)
    {

    if (err)
    console.log("Error inserting : %s ",err );

    res.redirect('/customers');

    });

    // console.log(query.sql); get raw query

    });
    };
    exports.save_edit = function(req,res){

    var input = JSON.parse(JSON.stringify(req.body));
    var id = req.params.id;

    req.getConnection(function (err, connection) {

    var data = {

    name: input.name,
    address : input.address,
    email : input.email,
    phone : input.phone

    };

    connection.query("UPDATE customer set ? WHERE id = ? ",[data,id], function(err, rows)
    {

    if (err)
    console.log("Error Updating : %s ",err );

    res.redirect('/customers');

    });

    });
    };

    exports.delete_customer = function(req,res){

    var id = req.params.id;

    req.getConnection(function (err, connection) {

    connection.query("DELETE FROM customerWHERE id = ? ",[id], function(err, rows)
    {

    if(err)
    console.log("Error deleting : %s ",err );

    res.redirect('/customers');

    });

    });
    };


    here's html code (ejs template) for listing the customer
    <%- include layouts/header.ejs %>














    <% if(data.length){

    for(var i = 0;i < data.length;i++) { %>









    <% }

    }else{ %>




    <% } %>

    NoNameAddressPhoneEmailAction
    <%=(i+1)%><%=data[i].name%><%=data[i].address%><%=data[i].phone%><%=data[i].email%>
    ">Edit
    ">Delete
    No user

    <%- include layouts/footer.ejs %>

    Well, actually 'm too lazy to put it all here...its gonna be a long scroll :(. pardon me for that. I think you can just download the Source herenodecrud and put a questions or issue on the Comment bellow.

    run the the source code :

    ubuntu@AcerXtimeline:~/hello_world$ node app.js
    http://localhost:4300/customers

    The source will produce things like these:

    Happy coding

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    ExampleofCRUDwithNode.js&amp;MySQL_MySQL

    ExampleofCRUDwithNode.js&MySQL_MySQL:NodeJS This time I'd like to share a basic and simple example of CRUD Operation in Node.js and MySQL. Its a lil bit hard to find tutorial Node.js n MySQL as poeple tend to use Mongoose instead of MySQL. Before we start, Please mind the env
    推薦度:
    標簽: js no my
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 91久久国产综合精品女同我 | 成人精品一区二区www | 日韩国产综合 | 欧美亚洲韩国 | 亚洲欧美综合图区官网 | 能看毛片的网站 | 一区二区三区高清不卡 | 草逼视频免费看 | 美女视频黄a视频免费全过程在线 | 亚洲三级一区 | 国产高清美女一级a毛片久久 | 国产成人精品免费视频大全五级 | 国产视频最新 | 精品国产免费观看一区高清 | 日本一区二区三区免费观看 | 精品国产91久久久久久久 | 多人伦精品一区二区三区视频 | 国产欧美在线观看不卡 | 国产亚洲欧美精品久久久 | 视频精品一区二区三区 | 成人毛片在线观看 | 日韩有码第一页 | 日韩在线免费视频观看 | 亚洲欧美视频一区二区三区 | 欧美色图一区 | 在线欧美日韩精品一区二区 | 中文字幕日韩有码 | 亚洲欧美另类专区 | 亚洲欧美另类自拍 | 国产 日韩 欧美 在线 | 日韩午夜在线视频 | 欧美日韩国产精品自在自线 | 欧美成人精品一级高清片 | 国产欧美一区二区三区视频 | 久久精品综合国产二区 | 国模双双大尺度炮交g0go | 国产精品一区二区久久精品 | 最新偷窥盗摄视频在线 | 午夜视频久久久久一区 | 小处雏高清一区二区三区 | 精品久久一区二区三区 |