javascript - I am trying to connect to connect to my SQL server database with node and hapi.js -
i trying test out connection sql server using node , hapi.js , run simple query on 1 of tables. new node , hapi trying figure out
so first folder structure follows have folder called api has routes in , doing routes get_aosdata.js works tested passing 'hello world' have query folder under api setting db connections in connection.js
this route looks
'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 soheader join customer b on a.custid = b.custid join soline c on a.ordnbr = c.ordnbr sotypeid = 'q'`, ((err, recordset) => { if (err) { console.log(err); } reply(recordset); })); } } }
i calling prereq connection code looks
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;
this server
'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}`); });
now error says pre @ position 0 not match of allowed types... clueless on error , cannot find fix anywhere. using hapi , mssql make call appreciated new @ node , hapi, have read lot on hapi , use it
this how doing hope 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 soheader join customer b on a.custid = b.custid join soline c on a.ordnbr = c.ordnbr 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 case in routes think
then server same
'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 totally wrong way how got mine work
Comments
Post a Comment