[英]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.