nodejs+express+mysql

2019-04-03  本文已影响0人  undefined404

var express = require('express'); //引入express模块

var mysql = require('mysql');    //引入mysql模块

var app = express();        //创建express的实例

var connection = mysql.createConnection({      //创建mysql实例

    host: 'localhost',

    user: 'root',

    password: '123456',

    port: '3306',

    database: 'littledream',

    useConnectionPooling: true

});

connection.connect(function (err) {

    if (err) {

        console.error('连接失败: ' + err.stack);

        return;

    }

    console.log('连接成功 id ' + connection.threadId);

});

app.get('/list', function (req, res) {

    var selectSQL = "SELECT * FROM websites";

    connection.query(selectSQL,function (err, results) {

        if (err) {

            console.log(err);

        }

        res.send(results);

    })

});

// connection.end();

app.listen(3000, function () {    ////监听3000端口

    console.log('Server running at 3000 port');

});

http://localhost:3000/list

上一篇下一篇

猜你喜欢

热点阅读