📜  如何使用节点 js 堆栈连接 mysql - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:05.703000             🧑  作者: Mango

代码示例3
var express = require('./lib/express');
    var app = express();
    var bodyParser = require('body-parser')
    var db = require('/db');
     app.get('/', function (req, res) {
        res.sendFile('/NodeProj/views/' + 'index.html');
    });
    /** bodyParser.urlencoded(options)
     * Parses the text as URL encoded data (which is how browsers tend to send form data from regular forms set to POST)
     * and exposes the resulting object (containing the keys and values) on req.body
     */
    app.use(bodyParser.urlencoded({
        extended: true
    }));

    /**bodyParser.json(options)
     * Parses the text as JSON and exposes the resulting object on req.body.
     */
    app.use(bodyParser.json());

    app.post('/process_form', function (req, res) {
        var response = {
            "firstname": req.body.fst_name,
            "email": req.body.fst_email,
            "password": req.body.fst_password
        };
        var query = connection.query('INSERT INTO register SET?',response,function(err,result){
            if(err) throw err;
            if(result) console.log(result);
        });
        res.end(JSON.stringify(response));
    });

    app.listen(8081);