<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專(zhuān)題視頻專(zhuān)題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專(zhuān)題1關(guān)鍵字專(zhuān)題50關(guān)鍵字專(zhuān)題500關(guān)鍵字專(zhuān)題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關(guān)鍵字專(zhuān)題關(guān)鍵字專(zhuān)題tag2tag3文章專(zhuān)題文章專(zhuān)題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專(zhuān)題3
        問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
        當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

        ExampleofCRUDwithNode.js&amp;amp;MySQL_MySQL

        來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-09 19:14:03
        文檔

        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
        推薦度:
        導(dǎo)讀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.

      1. I'm using Ubuntu
      2. NPM, Express
      3. MySQL for Node
      4. 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

        聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。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
        推薦度:
        標(biāo)簽: js no my
        • 熱門(mén)焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門(mén)推薦

        專(zhuān)題
        Top
        主站蜘蛛池模板: 国产美女亚洲精品久久久综合| 成人免费777777| 亚洲乱码精品久久久久..| 黄色一级毛片免费| 亚洲AⅤ无码一区二区三区在线| 国产精品亚洲а∨天堂2021| 免费无码黄动漫在线观看| 亚洲av永久中文无码精品综合 | 亚洲中久无码永久在线观看同| 美女被爆羞羞网站免费| 久久精品国产精品亚洲下载| kk4kk免费视频毛片| 亚洲国产精品久久久久久| 最近中文字幕完整版免费高清| 亚洲国产综合第一精品小说| 成人免费在线视频| 一级做a爰片久久毛片免费陪| 国产成人精品日本亚洲专区61 | 亚洲精品无码久久久久AV麻豆| 国产成人精品免费大全| 久久精品国产亚洲av成人| 8x成人永久免费视频| 国产人成亚洲第一网站在线播放| 免费高清资源黄网站在线观看 | 添bbb免费观看高清视频| 亚洲无码在线播放| 无码人妻丰满熟妇区免费| 亚洲人成片在线观看| 亚洲A∨精品一区二区三区| 日韩精品无码免费专区午夜 | 国产精品99久久免费观看| 亚洲字幕在线观看| 免费人成视频在线观看不卡| 精品国产污污免费网站 | 亚洲综合另类小说色区色噜噜| 黄网站免费在线观看| 亚洲色偷偷色噜噜狠狠99| 日韩一卡2卡3卡4卡新区亚洲| 波多野结衣在线免费视频| 一级女性全黄生活片免费看| 亚洲最大在线观看|