Why api deployment failed with express, typescript, mysql, firebase-admin

42 views Asked by At

First of all, i'm Korean so please understand my English mistakes..

I wanted to test deployment of my first backend api server(in dev branch), but every deployment I've tried has failed. (test in local(build and start pm2) is always success)

Can you guys tell me why my api deployment failed

About my project

  1. nodejs(v18), express, Typescript, cloudType(deploy platform from Korea)
  2. authentication: firebase-admin
  3. database: mysql(in aws RDS)
  4. storage: aws S3
  5. other package: cors, cross-env, pm2, winston, winstom-daily-rotate-file, helmet, hpp

server.js

import dotenv from "dotenv";
dotenv.config();
import express from "express";
import cors from "cors";
import morgan from "morgan";
import categoryRouter from "./routers/categoryRouter";
import postRouter from "./routers/postRouter";
import sequelize from "./database";
import { Category, Choice, Post } from "./models";
import logger from "./logger";
import helmet from "helmet";
import hpp from "hpp";
const PORT = process.env.PORT || 4000;

const app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: true })); 

app.use(
    cors({
        origin: `http://localhost:3000`,
        credentials: true,
        optionsSuccessStatus: 200,
    })
);

if (process.env.NODE_ENV === "production") {
    app.use(morgan("combined"));
    app.use(
        helmet({
            contentSecurityPolicy: false,
            crossOriginEmbedderPolicy: false,
        })
    ); 
    app.use(hpp());
} else {
    app.use(morgan("dev"));
}

app.use("/posts", postRouter);
app.use("/categories", categoryRouter);

app.use((req, res, next) => {
    const error = new Error(`${req.method} ${req.url} failed`);
    logger.error(error.message);
    res.status(404).json(error.message);
});
// association
Post.belongsTo(Category, {
    constraints: true,
    as: "category",
    onDelete: "CASCADE",
});
Category.hasMany(Post, { sourceKey: "id", foreignKey: "categoryId" }); 
Choice.belongsTo(Post, { constraints: true, as: "post", onDelete: "CASCADE" });
Post.hasMany(Choice, { sourceKey: "id", foreignKey: "postId" }); 

const handleListening = () =>
    logger.info(`✅ Server listenting on port http://localhost:${PORT} `);

sequelize
    .sync()
    .then(() => {
        app.listen(PORT, handleListening);
    })
    .catch((error) => logger.error(error));

package.json(npm run build && npm run start is success when I tried in local)

{
    "devDependencies": {
        "@types/cors": "^2.8.13",
        "@types/express": "^4.17.17",
        "@types/hpp": "^0.2.2",
        "@types/morgan": "^1.9.4",
        "@types/multer": "^1.4.7",
        "@types/multer-s3": "^3.0.0",
        "@types/node": "^18.15.11",
        "nodemon": "^2.0.22",
        "ts-node": "^10.9.1",
        "typescript": "^5.0.3"
    },
    "dependencies": {
        "@aws-sdk/client-s3": "^3.245.0",
        "body-parser": "^1.20.1",
        "cors": "^2.8.5",
        "cross-env": "^7.0.3",
        "dotenv": "^16.0.3",
        "express": "^4.18.2",
        "firebase-admin": "^11.5.0",
        "helmet": "^7.0.0",
        "hpp": "^0.2.3",
        "morgan": "^1.10.0",
        "multer": "^1.4.5-lts.1",
        "multer-s3": "^3.0.1",
        "mysql2": "^2.3.3",
        "pm2": "^5.3.0",
        "sequelize": "^6.28.0",
        "uuid": "^9.0.0",
        "winston": "^3.8.2",
        "winston-daily-rotate-file": "^4.7.1"
    },
    "name": "myServer",
    "version": "1.0.0",
    "main": "server.js",
    "scripts": {
        "dev": "nodemon --watch src --exec 'npx' 'ts-node' ./src/server.ts",
        "build": "tsc",
        "start": "cross-env NODE_ENV=production pm2 start ./dist/server.js -i -1"
    },
    "author": "hello",
    "license": "MIT",
    "description": "here is description"
}

tsconfig.json

{
    "compilerOptions": {
        "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
        "module": "commonjs" /* Specify what module code is generated. */,
        "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
        "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
        "strict": true,
        "skipLibCheck": true,
        "typeRoots": ["./node_modules/@types"],
        "outDir": "dist",
        "removeComments": true
    },
    "include": ["src"],
    "exclude": ["node_modules"]
}

firebase-config.ts

import admin from "firebase-admin";
const params = {
    type: process.env.FIREBASE_ADMIN_TYPE,
    projectId: process.env.FIREBASE_ADMIN_PROJECT_ID,
    privateKeyId: process.env.FIREBASE_ADMIN_PRIVATE_KEY_ID,
    privateKey: process.env.FIREBASE_ADMIN_PRIVATE_KEY,
    clientEmail: process.env.FIREBASE_ADMIN_CLIENT_EMAIL,
    clientId: process.env.FIREBASE_ADMIN_CLIENT_ID,
    authUri: process.env.FIREBASE_ADMIN_AUTH_URI,
    tokenUri: process.env.FIREBASE_ADMIN_TOKEN_URI,
    authProviderX509CertUrl:
        process.env.FIREBASE_ADMIN_AUTH_PROVIDER_X509_CERT_URL,
    clientC509CertUrl: process.env.FIREBASE_ADMIN_CLIENT_X509_CERT_URL,
};

admin.initializeApp({
    credential: admin.credential.cert(params),
});

export default admin;

github issues: more information of my files

0

There are 0 answers