我的express应用程序在本地主机上运行良好,但在Heroku上不起作用.

the line is

app.use("/api/product", require("./routes/product"))

这是密码

const express = require("express");
const app = express();

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


app.get("/", (req, res) => {
    res.send("responded")
});

app.use(express.json())

app.use("/api/product", require("./routes/product"))


app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`);
});

product.js

const express = require("express");
const router = express.Router();

router.get("/", async (req, res) => {
    try {
        res.json({
            status: 200,
            message: "Data has been successfully fetched"
        });
    }
    catch (error) {
        console.log(error);
        return res.status(400).send("server error")
    }
})

module.exports = router;

package.json

{
  "name": "backend-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.3"
  }
}

文件夹 struct

推荐答案

你想换个地方.否则,您将永远无法丰富您的api,因为第一个api将捕获所有请求.

const express = require("express");
const app = express();

const port = process.env.PORT || 5000;
app.use(express.json())

app.use("/api/product", require("./routes/product"))
app.get("/", (req, res) => {
    res.send("responded")
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`);
});

Javascript相关问答推荐

调用SEARCH函数后,程序不会结束

使用JavaScript在ionic Web应用程序中更新.pane和.view的背景 colored颜色

当在select中 Select 选项时,我必须禁用不匹配的 Select

按下同意按钮与 puppeteer 师

如何在Javascript中使用Go和检索本地托管Apache Arrow Flight服务器?

用JavaScript复制C#CRC 32生成器

配置WebAssembly/Emscripten本地生成问题

为什么按钮会随浮动属性一起移动?

如何在 cypress 中使用静态嵌套循环

如何在输入元素中附加一个属性为checkbox?

使用GraphQL查询Uniswap的ETH价格

如何迭代叔父元素的子HTML元素

为什么我的getAsFile()方法返回空?

Eval vs函数()返回语义

使用带有HostBinding的Angular 信号来更新样式?

VSCode中出现随机行

如何让SVG图标在被点击和访问后改变 colored颜色 ,并在被访问后取消点击时恢复到原来的 colored颜色 ?

在高位图中显示每个y轴系列的多个值

使用Perl Selify::Remote::Driver执行Java脚本时出错

将延迟加载的模块转换为Eager 加载的模块