简体   繁体   English

将带有Express.js的SQL Server API部署到Heroku

[英]Deploy SQL Server api with Express.js to Heroku

I'm very new with Express.js, but right now i've created an api of SQL Server DB. 我对Express.js非常陌生,但是现在我已经创建了SQL Server DB的api。 It works fine on localhost, but now, i've deployed on Heroku. 它在localhost上运行良好,但是现在,我已经在Heroku上进行了部署。 While my CMD prompt is open, my api works fine, but when it's close, i get an Internal Server Error. 当我的CMD提示符打开时,我的api正常工作,但是当它关​​闭时,我收到内部服务器错误。

Previously, i've created a test using Mongo as DB, mongoose and deployed to Heroku an the api still working even when the prompt isn't open. 以前,我使用Mongo作为数据库,猫鼬创建了一个测试,并将其部署到Heroku,即使提示未打开,该api仍然可以正常工作。 Someone knows if i have to create another .js document just like in mongoose or else to keep working my api? 有人知道我是否必须像创建猫鼬一样创建另一个.js文档,否则要保持我的api正常工作?

This is my code on the .js document (server.js): 这是我在.js文件(server.js)上的代码:

const express = require('express');
const bodyParser = require('body-parser');
const sql = require('mssql');
const app = express();

app.use(bodyParser.json());

app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
    next();
});


const dbConfig = {
    user: "daUser",
    password: "daPass",
    server: "daServer",
    database: "DaDB"
}

const executeQuery = function (res, query) {
    sql.connect(dbConfig, function (err) {
        if (err) {
            console.log(err);
            res.send(err);
        }
        else {
            // create Request object
            var request = new sql.Request();
            // query to the database
            request.query(query, function (err, result) {
                if (err) {
                    console.log(err);
                    res.send(err);
                }
                else {
                    res.send(result);
                }
            });
        }
    });
}

app.get("/api/HolidayBaseApi", function (req, res) {
    var query = "SELECT * FROM [HolidaysBase]";
    executeQuery(res, query);
})

const PORT = process.env.PORT || 8080

app.listen(PORT, () => {
    console.log("App now running on port", PORT);
});

My package.json next: 我的package.json接下来:

    {
  "name": "holidaysbaseapi",
  "version": "1.0.0",
  "description": "Api of Holidays",
  "main": "server.js",
  "scripts": {
    "start": "node server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Chuck Villavicencio",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.18.3",
    "express": "^4.16.4",
    "mssql": "^4.3.0"
  },
  "engines": {
    "node": "8.11.3",
    "npm": "5.6.0"
  }
}

On Heroku i've installed the Heroku CLI; 在Heroku上,我安装了Heroku CLI; logged in, Clone the repository and deployed my changes. 登录后,克隆存储库并部署我的更改。

I'm using Express.js, Node, SQL Server and Heroku 我正在使用Express.js,Node,SQL Server和Heroku

The problem is that your SQL server db is created on you local machine and heroku can't connect to it. 问题是您的SQL Server数据库是在本地计算机上创建的,而heroku无法连接到它。 You can you the postgresDB provided by heroku or create the sql server db in any provider around the internet and replace the dbConfig with the configs of that db 您可以使用heroku提供的postgresDB,也可以在互联网上的任何提供程序中创建sql服务器数据库,并将dbConfig替换为该数据库的配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM