本文介绍了我正在尝试使用node和hapi.js进行连接以连接到我的SQL Server数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用node和hapi.js测试到我的sql服务器的连接,并在我的一张表上运行一个简单的查询.我对Node和Hapi还是很陌生,所以我只是想弄清楚这一点

I am trying to test out connection to my sql server using node and hapi.js and run a simple query on one of my tables. I am fairly new to node and Hapi so I am just trying to figure this out

所以首先我的文件夹结构如下:我有一个名为api的文件夹,其中有路由,这就是我正在执行路由get_aosdata.js的位置. api下的查询文件夹,这是我在connection.js中设置数据库连接的地方

so first my folder structure is as follows I have a folder called api which has routes in it and that is where I am doing my routes get_aosdata.js this works I tested it just passing back 'hello world' then I have a query folder under api this is where I am setting my db connections in connection.js

这是我当前的路线

'use strict';
const query = require('../query/connection');

module.exports = {
    method: 'GET',
    path: '/api/query/{id}',
    config: {
        pre: [
            { method: query.getSqlConnection, assign: 'db' }
        ],
        handler: (request, reply) => {
                const request = new db.Request();
                request.query(`SELECT a.OrdNbr, a.sotypeid, a.user6, a.lupd_datetime, a.user3, a.crtd_user, a.S4Future01, a.slsperid, a.totmerch, a.CustOrdNbr from SOHeader a
                              join customer b
                              on a.CustID = b.CustId
                              join SOLine c
                              on a.OrdNbr = c.OrdNbr
                              where sotypeId = 'Q'`, ((err, recordset) => {

                                if (err) {
                                    console.log(err);
                                }
                                reply(recordset);
                              }));

        }
    }
}

我正在调用prereq进行连接,这就是这段代码的样子

I am calling to a prereq to do my connection this is what this code looks like

const sql = require('mssql');

const dbConfig = {
    server: 'myserver',
    database: 'mydatabase',
    user: 'myuser',
    password: 'mypassword'
}

const getSqlConnection = (request, reply) => {
    sql.connect(dbConfig, ((err) => {
                if (err) {
                    console.log(err);
                }
    }));
            return reply();
}

module.exports = getSqlConnection;

这是我的服务器

'use strict';

const Hapi = require('hapi');

const server = new Hapi.Server();

server.connection({ port: 3001, host: 'localhost' });

server.route(require('./api/routes/get_AOSdata'));

server.start((err) => {
    if(err) {
        throw err;
    }

    console.log(`Server running at: ${server.info.uri}`);
});

现在我的错误说位置0处的pre与任何允许的类型都不匹配...我对这个错误一无所知,而且在任何地方都找不到修复程序.我正在使用hapi和mssql进行呼叫,将不胜感激,因为我是node和hapi的新手,但是我在hapi上读了很多书,真的很想使用它

now my error says pre at position 0 does not match any of the allowed types... I am clueless on this error and I cannot find a fix anywhere. I am using hapi and mssql to make the call any help would be appreciated I am new at node and hapi, however I have read a lot on hapi and would really like to use it

推荐答案

这就是我的做法,希望对您有帮助

this is how I am doing it so I hope it helps

'use strict';
const sql = require('mssql');
const async = require('asyncawait/async');
const await = require('asyncawait/await');
const boom = require('boom');

const dbConfig = {
    server: 'myserver',
    database: 'mydb',
    user: 'myuser',
    password: 'mypass'
}

const conn = new sql.ConnectionPool(dbConfig);

module.exports = {
    method: 'GET',
    path: '/api/query/{id}',
    config: {
        handler: (request, reply) => {


            conn.connect().then(() => {
              req.query(``SELECT a.OrdNbr, a.sotypeid, a.user6, a.lupd_datetime, a.user3, a.crtd_user, a.S4Future01, a.slsperid, a.totmerch, a.CustOrdNbr from SOHeader a
                          join customer b
                          on a.CustID = b.CustId
                          join SOLine c
                          on a.OrdNbr = c.OrdNbr
                          where sotypeId = 'Q'`).then((data) => {
                  reply(data);
                  conn.close();
                })
              .catch((err) => {
               reply(boom.badRequest(err.message, err));
                conn.close();
              });
          })
          .catch((err) => {
              reply(boom.badRequest(err.message, err));
          });
        }
    }
}

在您的情况下,这就是您认为的路线

in your case this would be in your routes I think

那么您的服务器将看起来像这样

then your server would look almost the same like this

'use strict';
const Hapi = require('hapi');
const boom = require('boom');

const server = new Hapi.Server();

server.connection({ port: 3001, host: 'localhost', routes: {cors: true} });

server.route(require('./api/routes/get_AOSdata'));

server.start((err) => {
    if(err) {
        boom.badRequest(err.message, err);
    }

    console.log(`Server running at: ${server.info.uri}`);
});

这可能是完全错误的方式,但这就是我的工作方式

this could be totally the wrong way but this is how I got mine to work

这篇关于我正在尝试使用node和hapi.js进行连接以连接到我的SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 23:58