如何使用node.js将html表单数据保存到sql server数据库

lx0bsm1f  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(297)

我用它来访问服务器,这是我在stackoverflow中找到的代码,但我不太明白我必须用端口、用户名和密码访问特定的服务器。我必须将html表单页面中的数据插入sql server数据库,但我无法理解这些代码的逻辑,这使我无法连接到sql。我需要解释,以及如何使用用户名密码端口数据库名称和服务器将数据连接并添加到sql

var express = require('express');
var async = require('async');
var bodyParser = require("body-parser");
var mssql = require('mssql');
var server = 'local';
var port = 00;
var app = express();

const connection = require('./config/database');

connection.connect(function (err){
    if (err) throw err;
    console.log("The server has successfully connected to the MSSQL database")
});

var home = require('./app/routes/home');
var login = require('./app/routes/users/login');
var register = require('./app/routes/users/register');

app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());

app.get('/', home);
app.get('/login/', login);
app.post('/register/', register);

app.listen(port, hostname, function(){
    console.log("My server is running on http://"+ hostname +":"+port);
});

这是给登记处的

module.exports = (function() {
    'use strict';

const connection = require('../../../config/database');
const User = require('../../models/User');

module.exports = function(req, res) {
    console.log(req.body.firstname);
    User.addUser(req.body.firstname, req.body.lastname, req.body.email, req.body.password,function(err,rows){
      if(err) {
        return res.status(500).json({
                'code':'500',
                'success':'false',
                'msg': err});
      } else {
        return res.status(200).json({
                'code':'200',
                 'success':'true',
                 'msg':'Bonjour '+rows[0].firstname+', vous avez bien été créé'
                    });
            }

        });
    };
    return router;
})();
addUser:function(firstname,lastname,email,password,callback){

    return db.query
        (
          "INSERT INTO `users` VALUES (?,?,?,?)"
+                [firstname,lastname,email,password],
            callback
        );
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题