LoginSignup
0
0

More than 1 year has passed since last update.

Express.JSアプリケーション Deploying to Vercel ⑤

Last updated at Posted at 2022-09-25

参考動画

完成したアプリケーション

image.png

ソースコード

How to create and deploy an Express.js app to Vercel?

設定ファイル

vercel.json
{
  "version": 2,
  "builds": [
    {
      "src": "app.js",
      "use": "@now/node"
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "app.js"
    }
  ]
}
app.js
require("dotenv").config();
const express = require("express");
const connectDB = require("./db/connect");
const app = express();
const path = require("path");
const tasksRoute = require("./routes/tasks");

const PORT = process.env.PORT || 5000;

// middleware
app.use(express.json());
app.use(express.static("./public"));

// routes
app.get("/", (req, res) => {
  res.sendFile("index.html", { root: path.join(__dirname, "public") });
});

app.use("/api/v1/tasks", tasksRoute);

const start = () => {
  try {
    connectDB(process.env.MONGO_URI);
    app.listen(PORT, () => {
      console.log(`server is listening on port ${PORT}`);
    });
  } catch (error) {
    console.log(error);
  }
};

start();

module.exports = app;
package.json
{
  "name": "free",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "engines": {
    "node": "14.x"
  },
  "scripts": {
    "start": "nodemon app.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dotenv": "^16.0.2",
    "express": "^4.18.1",
    "mongoose": "^6.6.1",
    "nodemon": "^2.0.20"
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0