使用CLI创建的带有MSSQL的Express应用无法正确连接到数据库

时间:2018-12-28 02:07:28

标签: node.js sql-server express

我使用Express CLI创建的应用程序有问题。请务必注意,我正在使用mssql来连接数据库。

当我使用npm start时,该应用程序启动没有问题,但是当我运行node ./bin/www时,该应用程序启动了,但是无法连接到数据库。我收到错误"Invalid server: undefined"

当我检查package.json文件时,我有:

  "scripts": {
    "start": "node ./bin/www",
    "debug": "set DEBUG=myapp:* & npm start",
    "nodemon": "npx nodemon"
  },

据我所知,运行npm startnode ./bin/www应该完全相同,但是由于某种原因,它不是。

关于这是一个问题的原因,我想使应用程序使用pm2来运行,因此,我需要能够从文件而不是从命令中运行该应用程序。

这是我的www文件:

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('mtloops-web-api:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort('3543');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

这是我的数据库连接文件:

const sql = require('mssql')
// const path = require('path')
require('dotenv').config()

var config = {
  user: process.env.DBUSERNAME,
  password: process.env.DBPASSWORD,
  server: process.env.HOST,
  database: process.env.DATABASE,
  connectionTimeout: 300000,
  requestTimeout: 300000,
  pool: {
    idleTimeoutMillis: 300000,
    max: 100
  },
  options: {
    encrypt: false
  }
}

async function sp (procedure, params) {
  const toReturn = await new sql.ConnectionPool(config).connect().then(pool => {
    const request = pool.request()
    Object.keys(params).forEach(key => {
      if (isNaN(params[key])) {
        request.input(key, sql.VarChar(8), params[key])
      } else {
        var numeric = parseInt(params[key])
        request.input(key, sql.Int, numeric <= 0 ? -1 : numeric)
      }
    })
    return request.execute(procedure)
  }).catch(error => {
    throw error.message
  })
  return toReturn
}

module.exports = {
  sp
}

0 个答案:

没有答案