應用node+express+mysql 實現簡單的增刪改查

來源:互聯網
上載者:User

標籤:test   更新   settings   格式   ima   lease   blog   管理   image   

記錄下來備忘

1、準備好webstrom編輯器,編輯器調整字型大小等在file->settings->editor下設定 註冊碼 來源網上:

2017.2.27更新
選擇“license server” 輸入:http://idea.imsxm.com/

2、準備好 mysql 因為我本機有安裝xampp 所以不需要單獨準備mysql資料庫 如果沒有需要安裝,安裝了mysql的Navicat Premium管理工具。

3、查看下本機node和express是否安裝 版本node -v  express --version

4 、開啟webstrom建立工程File->New->Project->nodejs  

手動更改views下支援的模板檔案格式,現在views下都是ejs檔案,修改 app.js

app.set(‘views‘, path.join(__dirname, ‘views‘));var template = require(‘art-template‘);template.config(‘base‘, ‘‘);template.config(‘extname‘, ‘.html‘);app.engine(‘.html‘, template.__express);app.set(‘view engine‘, ‘html‘);

建立資料庫 略過

在routes下建立db.js 串連資料庫

//db.js//串連mysqlvar mysql = require(‘mysql‘);var pool = mysql.createPool({    host:‘localhost‘,    user:‘root‘,    password:‘‘,    database:‘testdb‘})function query(sql, callback) {    pool.getConnection(function (err, connection) {        // Use the connection        connection.query(sql, function (err, rows) {            callback(err, rows);            connection.release();//釋放連結        });    });}exports.query = query;

在views下建立users.html

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>{{title}}</title></head><body><table>    <tr>        <th>編號</th>        <th>姓名</th>        <th>年齡</th>        <th>操作</th>    </tr>    {{each datas as value index}}    <tr>        <td>{{value.id}}</td>        <td>{{value.name}}</td>        <td>{{value.age}}</td>    </tr>    {{/each}}</table></body></html>

在routes下建立user.js

var express = require(‘express‘);var router = express.Router();var db = require(‘./db.js‘);/* GET users listing. */router.get(‘/‘, function(req, res, next) {  db.query(‘select * from userinfo‘,function(err,rows){    if(err){      res.render(‘users‘,{title:‘Express‘,datas:[]});    }else{      res.render(‘users‘,{title:‘Express‘,datas:rows});    }  })});module.exports = router;

可以簡單遍曆資料庫欄位到頁面。

    

 

應用node+express+mysql 實現簡單的增刪改查

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.