简体   繁体   English

如何制作 node.js api 并将其部署到 heroku(postgresql 或 ZF8C0921E52FB066B4D69E8DBBE0230B4D69E8DBBE数据库)

[英]How to make a node.js api and deploy it to heroku (postgresql or mySQL database)

I am working on rest api with node.js and express that connects to the client side I have found a tutorial here https://www.taniarascia.com/node-express-postgresql-heroku/ and everything works fine but it will not deploy to heroku and I do not know why because everything works fine when I run it on localhost. I am working on rest api with node.js and express that connects to the client side I have found a tutorial here https://www.taniarascia.com/node-express-postgresql-heroku/ and everything works fine but it will not deploy to heroku 我不知道为什么,因为当我在本地主机上运行它时一切正常。 Why is this happening?为什么会这样?

Here is my code这是我的代码

index.js index.js

const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const {pool} = require("./config");
const { get } = require("mongoose");
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cors());
const getBooks = (req, res) => {
    pool.query('SELECT * FROM books', (err, results) => {
        if(err){
            throw err;
        }
        res.status(200).json(results.rows);
    });
}
const addBook = (req, res) => {
    const {author, title} = req.body;
    pool.query(
        'INSERT INTO books (author, title) VALUES ($1, $2)',
        [author, title],
        (err) => {
            if(err){
                throw err;
            }
            res.status(201).json({status: "success", message: "Book added."});
        },
    )
}

app.route("/books").get(getBooks).post(addBook);
app.listen(process.env.PORT || 8000, () => {
    console.log(`Server listening`);
})

config.js配置.js

require("dotenv").config();
const {Pool} = require("pg");
const isProduction = process.env.NODE_ENV === "production";
const connectionString = `postgresql://${process.env.DB_USER}:${process.env.DB_PASSWORD}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_DATABASE}`;
const pool = new Pool({
    connectionString: isProduction ? process.env.DATABASE_URL : connectionString,
    ssl: isProduction,
});
module.exports = {pool};

init.sql init.sql

CREATE TABLE books (
    ID SERIAL PRIMARY KEY,
    author VARCHAR(255) NOT NULL,
    title VARCHAR(255) NOT NULL
);

INSERT INTO books (author, title)
VALUES ('J.K. Rowling', 'Harry Potter');

I'd have a look at this, if your app works fine locally but not on heroku it's probably a deployment issue: https://devcenter.heroku.com/articles/getting-started-with-nodejs我会看看这个,如果你的应用程序在本地运行良好但在 heroku 上运行良好,这可能是一个部署问题: https://devcenter.heroku-jsnodes/

Specifically, l think you probably need a Procfile which is like an extra file that Heroku needs to deploy.具体来说,我认为您可能需要一个 Procfile,它就像 Heroku 需要部署的额外文件。 Assuming you got set up using the cli ( https://devcenter.heroku.com/categories/command-line ) you can get more detail on how your app is failing by getting the logs using 'heroku logs -t' inside the folder you deployed from.假设您使用 cli ( https://devcenter.heroku.com/categories/command-line ) 进行设置,您可以通过在文件夹中使用“heroku logs -t”获取日志来获取有关应用程序如何失败的更多详细信息你部署的。

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

相关问题 如何使用mysql数据库在node.js中制作登录API - How to make login API in node.js using mysql database 如何使用mysql数据库在node.js中创建登录表单 - How to make login form in node.js using mysql database 在 heroku 上部署了 MySQL 数据库的 Node.js 应用程序不断崩溃 - Node.js app with a MySQL database deployed on heroku keeps crashing Heroku Node.js远程Mysql数据库IP地址 - Heroku Node.js Remote Mysql Database IP Address 尝试将 MySQL 和 Node.js 应用程序部署到 heroku,在本地运行 heroku 时没有部署,成功但没有浏览器 - Attempting to deploy MySQL and Node.js app to heroku, no deployment when running heroku local, success but no browser 如何在运行Node.js应用程序之前确保MySQL数据库存在 - How to make sure a MySQL database exists before running Node.js app 无法从 Heroku 或 Replit 网站连接到存储在 aws 上的数据库。 (mysql2, node.js) - Cannot connect to database stored on aws from Heroku or Replit websites. (mysql2, node.js) 如何将本地 MySQL 数据库部署到 Heroku - How to deploy local MySQL database to Heroku 如何使用 MySQL 数据库在 Heroku 上部署 Go 服务器? - How to deploy Go server on Heroku with a MySQL database? 如何从 Node.js 向 MySQL 数据库中插入汉字? - How to insert Chinese characters into a MySQL database from Node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM